比方说我要读取.txt文件中的内容,我只要将红色下划线的地方更改,其他数据保留,
不是生成新的一行,当文件中下划线位置没有数据时,第一次写入怎么写
怎么写,白白白白白啊?

比方说我要读取.txt文件中的内容,我只要将红色下划线的地方更改,其他数据保留,
不是生成新的一行,当文件中下划线位置没有数据时,第一次写入怎么写
怎么写,白白白白白啊?

我不知道这么用ofstream和ifstream同时操作一个文件,只好先读了存起来,操作完再放回去
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
string str,text[1000];
int count = 0;
ifstream in("test.txt");
while (1) {
getline(in, str);
text[count++] = str; //先用text存取文本内容
if (in.eof())
break;
}
in.close();
/*在这里对text进行修改*/
text[1].replace(2, 5, "12345"); //比如将第2行的,第3到5的字符改为12345
ofstream out("test.txt");
for (int i = 0; i < count; i++) //把这些数据重新输出到文本中
out << text[i] << endl;
out.close();
return 0;
}