Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么此行不输出我的文本文件? 我现在正在读一本教我C++的书,我遇到了一个问题。我浏览了一下互联网,想看看是否能找到答案,但我似乎不太理解它们。我写了这个代码 #include <iostream> #include <string> #include <fstream> using namespace std; int main() { // Writing the poem string poem = "\n\tI never saw a man who looked"; poem.append("\n\tWith such a wistful eye"); poem.append("\n\tUpon that little tent of blue"); poem.append("\n\tWhich prisoners call the sky"); // More stuff ofstream writer("poem.txt"); if(!writer) { cout << "Error opening file for output" << endl; return -1; // Signal a termination } writer << poem << endl; writer.close(); // Teminates the program return 0; }_C++ - Fatal编程技术网

为什么此行不输出我的文本文件? 我现在正在读一本教我C++的书,我遇到了一个问题。我浏览了一下互联网,想看看是否能找到答案,但我似乎不太理解它们。我写了这个代码 #include <iostream> #include <string> #include <fstream> using namespace std; int main() { // Writing the poem string poem = "\n\tI never saw a man who looked"; poem.append("\n\tWith such a wistful eye"); poem.append("\n\tUpon that little tent of blue"); poem.append("\n\tWhich prisoners call the sky"); // More stuff ofstream writer("poem.txt"); if(!writer) { cout << "Error opening file for output" << endl; return -1; // Signal a termination } writer << poem << endl; writer.close(); // Teminates the program return 0; }

为什么此行不输出我的文本文件? 我现在正在读一本教我C++的书,我遇到了一个问题。我浏览了一下互联网,想看看是否能找到答案,但我似乎不太理解它们。我写了这个代码 #include <iostream> #include <string> #include <fstream> using namespace std; int main() { // Writing the poem string poem = "\n\tI never saw a man who looked"; poem.append("\n\tWith such a wistful eye"); poem.append("\n\tUpon that little tent of blue"); poem.append("\n\tWhich prisoners call the sky"); // More stuff ofstream writer("poem.txt"); if(!writer) { cout << "Error opening file for output" << endl; return -1; // Signal a termination } writer << poem << endl; writer.close(); // Teminates the program return 0; },c++,C++,我认为具体的问题是,这个行写入器流类写入文件而不是屏幕,以便将文件内容写入程序,然后使用类ifstream 在程序中,如果要将文本写入文件,请读回程序: 在写入writer后立即关闭文件后添加此代码。关闭: 或者简单地使用fstream类的对象执行一次两个任务:写/读 代码看起来不错。文件被创建了,但它只是空的?代码工作正常,那么有什么问题吗?添加问题,而不是这么长的问题story@AndyG我用GEdit和VimJust检查了文件以防万一?。这首诗在里面。文件生成似乎有效。@joeldesan

我认为具体的问题是,这个行写入器流类写入文件而不是屏幕,以便将文件内容写入程序,然后使用类ifstream

在程序中,如果要将文本写入文件,请读回程序:

在写入writer后立即关闭文件后添加此代码。关闭:

或者简单地使用fstream类的对象执行一次两个任务:写/读


代码看起来不错。文件被创建了,但它只是空的?代码工作正常,那么有什么问题吗?添加问题,而不是这么长的问题story@AndyG我用GEdit和VimJust检查了文件以防万一?。这首诗在里面。文件生成似乎有效。@joeldesante:我不明白。那么问题出在哪里?@joeldesante:你能解释一下为什么你认为会发生这种情况吗?您的心智模型中缺少了一些关于流如何工作在C++中的内容,我想尝试修复:
ifstream inFile("poem.txt");
string sLine;

while(getline(inFile, sLine))
    cout << sLine << endl;
inFile.close();