问题在下面。我使用的是Microsoft Visual Studio,accessories.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;
}
答案 0 :(得分:1)
您可以使用此程序生成测试文件。无论生成该文件的位置如何,输入文件都必须存在。就我而言,它与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;
};
答案 1 :(得分:0)
在程序中添加files
,以查看程序运行时所在目录的路径。从那里应该可以找出文件的正确路径。