使用fstream读后写 代码> FStuts C++中的对象可以用于读和写,使用相同的流。 我已经成功地能够首先写入流,然后从中读取。但是,如果我尝试再次写入该文件,则不会影响该文件
以下是使用MinGw在windows上成功编译的代码示例:使用fstream读后写 代码> FStuts C++中的对象可以用于读和写,使用相同的流。 我已经成功地能够首先写入流,然后从中读取。但是,如果我尝试再次写入该文件,则不会影响该文件,c++,io,fstream,C++,Io,Fstream,以下是使用MinGw在windows上成功编译的代码示例: int main() { std::string path="file.txt"; std::fstream fs(path.c_str()); int buffSize=100; int bytesRead=0; char* buffer=new char[buffSize]; fs.write("hello", 5); fs.seekp(0, std::ios::beg);
int main()
{
std::string path="file.txt";
std::fstream fs(path.c_str());
int buffSize=100;
int bytesRead=0;
char* buffer=new char[buffSize];
fs.write("hello", 5);
fs.seekp(0, std::ios::beg);
fs.read(buffer, buffSize);
bytesRead=fs.gcount();
for(int i=0;i<bytesRead;i++) {std::cout << buffer[i];}
std::cout << "\n";
fs.clear();
fs.seekp(1, std::ios::beg);
fs.write("E", 1);
std::cout << "fail: " << fs.fail() << "\n";
delete[] buffer;
}
程序输出:
helloAA
fail: 0
运行程序后,在文本编辑器中查看文件,可以看出最终内容是:
helloAA
“E”的最终文字尚未生效,这是为什么?我如何修复它
编辑:
在按照用户0x499602D2的建议再次编写之前,我尝试使用了fs.clear()
。还添加了一行打印输出是否设置了failbit或badbit,并更新了程序输出。最后的文件内容保持不变,但问题仍然存在。(更多详细的答案来自我在问题评论中发布的内容)
您需要对输出流对象(从ostream派生)调用
flush()
,以便将数据实际写入输出流。有关flush()的更多信息可在GCC 4.9.0和VS2013中找到。这项工作
注:- seekg用于移动读取指针
- seekp用于移动写入指针
fs.seekp(0,std::ios::beg)行中的示例代码中代码>需要重新设置。没有问题,因为读取指针尚未移动(在移动之前没有读取)
代码:
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
std::string path=“H:\\save.txt”;
int buffSize=100;
int字节读取=0;
char*buffer=新字符[buffSize];
std::fstream fs(path.c_str());
财政司司长写(“你好”,5);
fs.flush();//刷新到磁盘文件
fs.seekg(0,std::ios_base::beg);//移动读取指针
fs.read(缓冲区、buffSize);
bytesRead=fs.gcount();
for(int i=0;istring data=“”;
string Newdata=“新数据”;
std::fstream输出_文件(文件名,ios::in | ios::out);
输出文件>>数据;//读取数据
output_file.seekg(0,ios::beg);//将点设置为零
使用fstream读取文件后,tellg和tellp指向-1。
为了能够使用fstream再次写入,只需调用fstream.clear(),它就会将读写指针重置到读取之前的位置
上面发布的解决方案只有fstream.clear()有效。clear()
第一次read()之后的流
@0x499602D2单凭这一点并不能解决问题,至少对我来说不是。它应该可以解决。读取整个文件会设置eof
标志,而clear
会将其清除,以便查找和写入成功。如果您在代码中进行了错误检查,您会看到失败的地方。您是否尝试调用flush()在写操作后的流对象上,我猜是在写了一次之后,数据没有被刷新到文件中。这个因素完全超出了我的头脑。没有考虑过很长时间的缓冲。不接受替代品!是最好的C++引用;CPLUS PLUS最近添加了一个“C++引用”。口号作为他们正在进行的搜索引擎优化游戏的一部分。
helloAA
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
std::string path = "H:\\save.txt";
int buffSize = 100;
int bytesRead = 0;
char* buffer = new char[buffSize];
std::fstream fs(path.c_str());
fs.write("hello", 5);
fs.flush(); // flushing to disk file
fs.seekg(0, std::ios_base::beg); // moving the read pointer
fs.read(buffer, buffSize);
bytesRead = fs.gcount();
for (int i = 0; i < bytesRead; i++) {
std::cout << buffer[i];
}
std::cout << "\n";
fs.clear();
fs.seekp(1, std::ios::beg);
fs.write("E", 1);
fs.flush(); // flushing to disk file
std::cout << "fail: " << fs.fail() << "\n";
delete[] buffer;
return 0;
}
string data="";
string Newdata="New Data";
std::fstream output_file(fileName, ios::in| ios::out);
output_file >> data; //read Data
output_file.seekg( 0, ios::beg );//set point to zero
output_file<<Newdata<<"\n"; //write new Data
output_file.close();