C ++使用getline从文件中读取后,我无法再写入txt文件 [英] C++ After I use getline to read from a file I can no longer write to the txt file

查看:105
本文介绍了C ++使用getline从文件中读取后,我无法再写入txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够写一个文本文件,然后使用getline,并且我不再写该文件.

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.

这篇关于C ++使用getline从文件中读取后,我无法再写入txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆