逐行修改c++中文本文件的内容

Modify the content of a text file in c++ line by line

本文关键字:文件 文本 修改 c++ 中文 逐行      更新时间:2023-10-16

我有一个文本文件,其中包含以下行

f 2533//1877 2535//1875 2639//1959 2629//1949

f 2641//1962 2643//1963 2622//1812 2215//1811

现在我想通过删除"//"并添加一个空格"它一定像

f 2533 1877 2535 1875 2639 1959 2629 1949

f 2641 1962 2643 1963 2622 1812 2215 1811

我如何实现这一点是c++

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 
ifstream myfile_in ("input.txt"); 
ofstream myfile_out ("output.txt");
string line; 
void find_and_replace( string &source, string find, string replace ) { 
size_t j; 
 for ( ; (j = source.find( find )) != string::npos ; ) { 
     source.replace( j, find.length(), replace ); 
  } 
 myfile_out << source <<endl; 
 cout << source << endl; 
} 
int main () { 
 if (myfile_in.is_open()) 
         { 
        int i = 0,j; 
        //string strcomma ; 
          // string strspace ; 
     while (! myfile_in.eof() ) 
             { 
              getline (myfile_in,line); 
            string strfind= "//"; 
            string strreplacewith = " "; 

            find_and_replace( line , strfind, strreplacewith ); 

          i++; 
       } 
        myfile_in.close(); 
     } 
        else cout << "Unable to open file(s) "; 
        system("PAUSE"); 
        return 0; 
      }