C++流找不到相对路径
C++ fstream Won't Find Relative Path
问题在下面。我使用的是Microsoft Visual Studio and 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;
};
向您的程序中添加system("dir")
,以查看程序运行时所处目录的路径。从那里您应该能够弄清楚文件的正确路径是什么。
相关文章:
- 如何使用cppcheck处理半相对包含路径
- Windows 和 Linux 之间的相对路径分辨率差异?
- 如何获取 CMake 单元测试的相对路径?
- Bazel:http_archive()中url的相对本地路径
- 在Visual Studio Code(.json配置)中设置相对于工作区路径的c / c ++项目的包含路径
- 从另一个应用启动我的应用时出现相对路径更改问题
- CMake 链接 2 个具有相对路径的库
- cmake find_package CMakeLists.txt 子文件夹中的相对路径
- 定义相对包括路径
- cmake:包括vs vs add_subDirectory:相对标头文件路径
- 使用相对标头包含路径重新分析 ASTUnit 失败
- 在可执行文件中使用带有符号链接的相对路径
- C++流找不到相对路径
- 如何将COM .tlb文件导入具有相对路径C++文件中?
- C++包括断开的相对路径
- CMake 安装具有相对路径的目标
- CMake为什么通过相对路径链接外部库
- Boost Filesystem flowly_caronical不会在没有父文件夹的情况下解决相对路径
- 在“其他包含目录”中添加的路径相对于什么
- 如何获得与给定的相对路径相对应的绝对库文件名