Jos*_*ley 9 c++ fstream file-management ifstream visual-studio
我知道这是一个noob问题,但是我之前和之后只想访问.txt文件,我曾经使用过Python.所有你需要做的就是确保txt文件在同一个目录中.我在下面有以下C++代码,但它没有找到我在桌面上保存的Numbers.txt文件.我在文件中的所有内容都是一行double类型的数字.我想要做的就是找到文件中所有数字的平均值.程序运行正常,但它不能正确打印输出.通过打印输出[0]检查打印到输出的内容后,我发现该文件没有将其内容复制到数组中.有人可以为我清除这个小问题,或者至少指出我正确的方向指向一个好的教程?
int main() {
cout << "Getting File Information..." << endl;
ifstream file;
char output[100];
//int x;
file.open("Numbers.txt", ios::in); // open file
cout << "Opened File Successfully ****************" << endl;
file >> output; // empty file contents into output
cout << output; // print out contents of file
cout << "Should have printed out results by now" << endl;
//file >> x;
file.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在调试模式下运行时,Visual Studio将工作目录设置为YourProjectDirectory\Debug\Bin.如果您的文本文件位于YourProjectDirectory中,则需要考虑该差异.
最简单的方法是在项目中包含文本文件,并将其构建操作(在"属性"窗口中)设置为"内容".
如果您正在讨论通过 F5 或 Debug / Start Debugging 在 Visual Studio 调试器中运行代码,则可以通过 Project / <项目名称> Properties / Configuration / Debugging /working Directory 设置程序的工作目录。
将文本文件放在某个目录中,并将工作目录设置为指向该目录。
归档时间: |
|
查看次数: |
54207 次 |
最近记录: |