【发布时间】:2010-11-29 15:29:47
【问题描述】:
我知道这是一个菜鸟问题,但我以前使用过 Python,例如,当您只想访问 .txt 文件时,您所要做的就是确保 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;
}
【问题讨论】:
标签: c++ visual-studio fstream ifstream file-management