C Fstream未显示写入的contenst
C++ fstream not displaying contenst written in
[编辑]将内容写入文件后,它不显示文本仅结束程序。我不是正确使用fstream吗?
fstream myfile;
int count = 0;
myfile.open("A3Problem2b.txt", ios::in | ios::out | ios::trunc);
if (!myfile.is_open())
{
cout << "Cannot open file - A3Problem2b.txt"<< endl;
exit(1);
}
char prompt[1000];
cout << "Enter a line of text with no punctuation <^Z to stop>:n";
cin.getline(prompt, 1000);
while ( !cin.eof() )
{
myfile << prompt << endl;
cin.getline(prompt, 1000);
}
myfile.getline(prompt, 1000);
while ( !myfile.eof() )
{
cout << prompt << "n";
myfile.getline(prompt, 1000);
}
myfile.close();
我认为您需要在从中阅读之前倒带流-fstream.seekg(0)
。
#include <iostream>
#include <fstream>
int main(int argc, char *argv[]) {
std::fstream myfile;
int count = 0;
myfile.open("kb.nt", std::ios::in | std::ios::out | std::ios::trunc);
if (!myfile.is_open())
{
std::cout << "Cannot open file - kb.nt"<< std::endl;
exit(1);
}
char prompt[1000];
std::cout << "Enter a line of text with no punctuation <^Z to stop>:n";
while ( std::cin.getline(prompt, 1000) )
{
myfile << prompt << std::endl;
}
myfile.flush();
myfile.seekg(0);
while ( myfile.getline(prompt, 1000) )
{
std::cout << prompt << "n";
}
myfile.close();
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- C Fstream未显示写入的contenst