c++ - C++ fstream 找不到相对路径
问题描述
问题如下。我使用的是 Microsoft Visual Studio,admissions.txt 与 .cpp、.h 和 .sln 文件位于同一文件夹中,但程序找不到相对路径。明确说明路径也不起作用。我只关心让 ifstream 现在正常工作。
/*
A new aquarium just opened up and your boss would like you to write a short program that allows him / her to calculate the number of tickets sold and money brought in for ticket sales.
There are different types of tickets you can buy : All - Access, Gold, and Silver.
The data for ticket sales will be stored in the file admissions.txt with the following format where the first column represents the ticket cost and the second the number of tickets sold.
150.00 89
56.50 300
45.25 450
The first line indicates that the ticket price is $150.00 and that 89 tickets were sold at that price.Output the total number of tickets sold and the total sale amount for ALL tickets.Format your output with two decimal places.
Sample input file :
226 1761
153 28513
62 35779
*/
include fstream
include iostream
include string
using namespace std;
int main()
{
ifstream inFileData;
string line1;
string line2;
string line3;
inFileData.open("admissions.txt"); //contains sample input
inFileData >> line1;
inFileData >> line2;
inFileData >> line3;
cout << line1;
cout << line2;
cout << line3;
inFileData.close();
system("pause");
return 0;
}
解决方案
您可以使用此程序生成测试文件。无论它在哪里生成所述文件,您的输入文件都必须在。就我而言,它与 VS 调试器的 .vcxproj 相关,并且在使用 .exe 时与 .exe 位于同一目录中。
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("relative_path_test.txt");
if (file.is_open()) {
file << "Test file";
}
file.close();
return 0;
};
推荐阅读
- javascript - 在监听事件外的时间获取事件内的变量
- javascript - 如何在运行时获取所有控制台消息以登录 Cordova iOS?
- php - 如何在android文本视图上查看php脚本的结果
- itext - 使用 itextsharp 删除 pdf 文档中的图像
- collation - 如果在mysql中出现非法混合排序(utf8_bin,none)和(utf8_bin,none)错误,如何更新edmx
- recursion - 子程序调用的参数“n-1”给出了语法错误
- c# - 无法读取 .NET Core API 项目中的应用设置
- gcc - Linker fails in sandbox when running through Bazel but works when sandboxed command is executed from the command line
- javascript - 如何通过由ng-repeat在angularjs中动态创建的id获取元素
- featuretools - 如何在特征工具中获取列名列表