-1

I have a text file that contains the following line

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

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

Now i want to edit the file by deleting "//" and adding a space " " which must be like

f 2533 1877 2535 1875 2639 1959 2629 1949

f 2641 1962 2643 1963 2622 1812 2215 1811

How can i achieve this is c++

Community
  • 1
  • 1

1 Answers1

-1
#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; 
      } 
Harikant
  • 269
  • 1
  • 6
  • To fix the `eof` issue, see http://stackoverflow.com/questions/2251433/checking-for-eof-in-stringgetline – anatolyg Apr 28 '14 at 06:38