简体   繁体   中英

C++ After I use getline to read from a file I can no longer write to the txt file

I am able to write to a text file then I use getline and I can no longer write to the file.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
 ifstream infile;
 fstream crypto;
 ofstream hacker;
 string tempString;
 string tempString2;
 string plaintext;
 string line;
 string dec;
.
.
.
crypto<<"hello";//write some stuff to file here
use getline(crypto, line, '\n')
crypto<<"hi";//Doesnt write to file anymore. 

Files have a single error status and a shared file-position indicator, which is used for both read and write operations.

If the error status is set, then neither read nor write operations will succeed on the file. Reading past the end of the file is one trigger that will cause the error status to be set, in this case until the file-position indicator is repositioned within the bounds of the file.

As there is only a single position indicator for both reading and writing, you need to reposition that indicator each time you switch between reading and writing the same file to ensure that you are performing the next operation at the position where you intend it to occur.

从您的代码中很难看到原样(每行之后尝试返回),但我想您已将文件访问权限设置为“ read”。

The file has entered an error state.

Once the file is in an error state then all io operations will be ignored until you reset the error state. So basically the above suggests that the getline operation failed in some way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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