简体   繁体   English

在C ++中使用fstream读取和附加文件

[英]reading and appending a file using fstream in C++

I am trying to read a file using fstream. 我正在尝试使用fstream读取文件。 Then when I reach the end I append the file and write some staff file 然后,当我到达最后,我附加文件并写一些员工文件

abc

I wrote sample code 我写了示例代码

#include<iostream>
#include<fstream>
#include<queue>

using namespace std;
int main(int argc, char **argv){
    fstream *binf;
    binf=new fstream("t.txt", ios::out|ios::in|ios::app);
    cout<<"EOF"<<EOF<<endl;
    while(true){
        cout<<"before peek"<<endl;
        cout<<"binf->tellg:"<<binf->tellg()<<endl;
        cout<<"binf->tellp:"<<binf->tellp()<<endl;
        cout<<"binf->peek()::int=("<<(int)binf->peek()<<")::char=("<<(char)binf->peek()<<")"<<endl;
        cout<<"after peek"<<endl;
        cout<<"binf->tellg:"<<binf->tellg()<<endl;
        cout<<"binf->tellp:"<<binf->tellp()<<endl;
        char c;
        if(binf->peek()==EOF){
            cout<<"file is not good"<<endl;
            break;
        }
        binf->get(c);
    }
    cout<<"binf->tellg:"<<binf->tellg()<<endl;
    cout<<"binf->tellp:"<<binf->tellp()<<endl;
    binf->seekg(3);
    binf->seekp(3);
    cout<<"binf->tellg:"<<binf->tellg()<<endl;
    cout<<"binf->tellp:"<<binf->tellp()<<endl;
    binf->put('H');
    cout<<"binf->tellg:"<<binf->tellg()<<endl;
    cout<<"binf->tellp:"<<binf->tellp()<<endl;
    binf->seekg(4);
    char c;
    binf->get(c);
    cout<<"c:"<<c<<endl;
    binf->clear();
    binf->close();
    delete binf;
    return 0;
}

This is the output I get from running this code 这是我从运行此代码得到的输出

EOF-1
before peek
binf->tellg:0
binf->tellp:0
binf->peek()::int=(97)::char=(a)
after peek
binf->tellg:0
binf->tellp:0
before peek
binf->tellg:1
binf->tellp:1
binf->peek()::int=(98)::char=(b)
after peek
binf->tellg:1
binf->tellp:1
before peek
binf->tellg:2
binf->tellp:2
binf->peek()::int=(99)::char=(c)
after peek
binf->tellg:2
binf->tellp:2
before peek
binf->tellg:3
binf->tellp:3
binf->peek()::int=(10)::char=(
)
after peek
binf->tellg:3
binf->tellp:3
before peek
binf->tellg:4
binf->tellp:4
binf->peek()::int=(-1)::char=(ÿ)
after peek
binf->tellg:-1
binf->tellp:-1
file is not good
binf->tellg:-1
binf->tellp:-1
binf->tellg:-1
binf->tellp:-1
binf->tellg:-1
binf->tellp:-1
c:

All I am trying to do is to fix the file and write at the end however, whenever I see the EOF at the end of file the file is no longer good to use even with peek() 我所要做的就是修复文件并在最后写,但是每当我在文件末尾看到EOF时,即使使用peek(),文件也不再好用

When you hit the end of file you put your fstream into an error state . 当您点击文件末尾时,您将fstream置于错误状态 When it's in an error state nothing will work until you clear the error state. 当它处于错误状态时,在清除错误状态之前没有任何工作。 So you need this 所以你需要这个

   if(binf->peek()==EOF){
        cout<<"file is not good"<<endl;
        binf->clear(); // clear the error state
        break;
   }

You do not need to clear the fstream just before you close it, that does nothing. 你不需要在关闭它之前清除fstream ,它什么都不做。

BTW good debugging technique, but you'll find this easier if you learn to use a proper debugger. BTW良好的调试技术,但如果你学会使用适当的调试器,你会发现这更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM