c++TXT文件内容修改,求大神赐教⁽˙³˙⁾◟(๑•́ ₃ •̀๑)◞⁽˙³˙⁾

用c++修改TXT文件的内容,比如
0000
1111
2222
3333
4444
5555
把3333改成aaaa。

代码如下:

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

int main() {
    string line;
    ifstream myfile("d:\\test.txt");

    vector<string> lines;

    //读文件各行到vector<string>中
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            lines.push_back(line);
        }
        myfile.close();
    }

    else
        cout << "Unable to open file";

    //循环并替换指定字符串
    for (vector<string>::iterator it = lines.begin(); it != lines.end(); ++it) {
        if (*it == "3333")
        {
            *it = "aaaa";
        }
    }


    //处理完毕后将结果写回到文件中
    ofstream myfileOut("d:\\test.txt");

    if (myfileOut.is_open())
    {
        for (vector<string>::iterator it = lines.begin(); it != lines.end(); ++it)
        {
            myfileOut << *it << endl;
        }
        myfileOut.close();
    }

    else
        cout << "Unable to open file";

    return 0;
}

说明
1: 考虑到题目中文件的行数可能不确定 ,所以采用了vector,便于动态扩展。
用到的都是C++标准库(std)的内容。
2: 代码中文件位置需要根据情况替换。
3: 运行前,请先关闭txt文件,程序运行完毕后,打开txt文件即可看到结果。

用心回答每个问题,如果有帮助,请采纳答案好吗,非常感谢~~~

MFC的话,用CString::Replace
标准C的话,用string.replace
http://www.cplusplus.com/reference/string/string/replace/