在程序中不能得到正确的输出
Cant get correct output in program
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
fstream file("file.txt");
file << "this is new line" << endl;
file.flush();
string c;
file >> c;
cout << c << endl;
file.close();
}
当我运行这个输出是空的,如果我删除行file << "this is new line" << endl;
我得到正确的输出,为什么?
通过写入文件,您将内部文件指针移动到文件的末尾。这意味着下次读取时,您将位于文件的末尾,因此不会读取任何内容。
看seek()
移动文件指针
相关文章:
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 显示错误输出的简单数组排序程序
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 如何从Windows应用程序输出到标准?
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么这个程序的输出不如预期
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ 每次运行程序时我都会"nan"输出的问题
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 而循环:简单的除法程序输出零,不明白为什么
- 如何在 html 页面中插入 cgi 应用程序的输出?
- C 中的输出程序无明显的原因是白色的
- C++:更改简单输出程序的字体
- C++ 简单的输入/输出程序 + 枚举器
- C++字母等效数字代码输出程序
- 编译输出程序集