Friday, September 23, 2011

How can I edit a text file in C++.?

What I want to do is change the file name of a file, then open it, change what it says and save it.

So say i have a file, example.txt. I would like to change it to example1.txt. Then in the file it says example. I would like to change that to example1. Then save it. Could someone give me the code for that. Thank you so much.How can I edit a text file in C++.?This seems like a straightforward problem, but there are some interesting things to consider to get it right. As you may realize, we're reinventing the wheel here, since sed can do this for you with a simple, 1-line command:



$ sed s/example/example1/ example.txt %26gt; example1.txt



However, if you want to code it yourself, or you don't have sed, it should look something like this:

#include %26lt;iostream%26gt;

#include %26lt;fstream%26gt;

#include %26lt;sstream%26gt;



using namespace std;



int main(int argc, char *argv[]) {

聽 聽 string file,f1,line,word,w1,ext;

聽 聽 stringstream ss;

聽 聽 unsigned n;

聽 聽 size_t p;



聽 聽 //

聽 聽 // Get input file name

聽 聽 //

聽 聽 cout %26lt;%26lt; %26quot;Text file name : %26quot;;

聽 聽 getline(cin,file);

聽 聽 ifstream infile(file.c_str());

聽 聽 if (infile.is_open()) {

聽 聽 聽 聽 //

聽 聽 聽 聽 // Input file found and opened. Get the word in the

聽 聽 聽 聽 // file to be modified, and the number to append

聽 聽 聽 聽 // to that word.

聽 聽 聽 聽 //

聽 聽 聽 聽 cout %26lt;%26lt; %26quot;Word to modify: %26quot;;

聽 聽 聽 聽 getline(cin,w1);

聽 聽 聽 聽 if (w1.size() != 0) {

聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 // Valid word entered, get number to append.

聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; %26quot;Number to append to word and new file name: %26quot;;

聽 聽 聽 聽 聽 聽 getline(cin,line);

聽 聽 聽 聽 聽 聽 ss.str(line);

聽 聽 聽 聽 聽 聽 if (ss %26gt;%26gt; n) {

聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 // Valid number entered, create output filename.

聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 ss.clear(); ss.str();

聽 聽 聽 聽 聽 聽 聽 聽 if ((p = file.find('.')) != string::npos) {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // Filename has an extension. Insert the

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // number before the extension.

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 f1 = file.substr(0,p);

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ext = file.substr(p+1);

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ss %26lt;%26lt; f1 %26lt;%26lt; n %26lt;%26lt; '.' %26lt;%26lt; ext;

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 f1 = ss.str();

聽 聽 聽 聽 聽 聽 聽 聽 } else {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // No '.' in filename, just append the number;

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ss %26lt;%26lt; file %26lt;%26lt; n;

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 f1 = ss.str();

聽 聽 聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; endl %26lt;%26lt; %26quot;Copying %26quot; %26lt;%26lt; file %26lt;%26lt; %26quot; to %26quot; %26lt;%26lt; f1 %26lt;%26lt; endl;

聽 聽 聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; %26quot;changing all \%26quot;%26quot; %26lt;%26lt; w1 %26lt;%26lt; %26quot;\%26quot; to \%26quot;%26quot;;

聽 聽 聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; w1 %26lt;%26lt; n %26lt;%26lt; %26quot;\%26quot;.%26quot; %26lt;%26lt; endl;

聽 聽 聽 聽 聽 聽 聽 聽 ofstream outfile(f1.c_str());

聽 聽 聽 聽 聽 聽 聽 聽 do {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 getline(infile,line);

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 if (infile) {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // Got a line from the input file

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ss.clear(); ss.str(line);

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 while (ss.good()) {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // Compare each word in this line with the word

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // to be modified. If it's the word to change,

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // write it out with the number appended. If not

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 // the word we're looking for, write it out as-is.

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 //

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 if (ss %26gt;%26gt; word) {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 if (word == w1) {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 outfile %26lt;%26lt; word %26lt;%26lt; n %26lt;%26lt; ' ';

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 } else {

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 outfile %26lt;%26lt; word %26lt;%26lt; ' ';

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 outfile %26lt;%26lt; endl;

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 聽 聽 聽 聽 } while (infile);

聽 聽 聽 聽 聽 聽 聽 聽 infile.close();

聽 聽 聽 聽 聽 聽 聽 聽 outfile.close();

聽 聽 聽 聽 聽 聽 } else {

聽 聽 聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; %26quot;invalid number.%26quot; %26lt;%26lt; endl;

聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 } else {

聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; %26quot;invalid word.%26quot; %26lt;%26lt; endl;

聽 聽 聽 聽 }

聽 聽 } else {

聽 聽 聽 聽 cout %26lt;%26lt; %26quot;error opening %26quot; %26lt;%26lt; file %26lt;%26lt; endl;

聽 聽 }

聽 聽 return(0);

}



#if 0



Sample run:



Text file name : example.txt

Word to modify: example

Number to append to word and new file name: 1



Copying example.txt to example1.txt

changing all %26quot;example%26quot; to %26quot;example1%26quot;.





Where:



$ cat example.txt

Here is an example of some text. The program

will read this example text, update every

occurrence of example to append the given

number, N, then write the modified file to

%26lt;filename%26gt;N.txt.



and :



$ cat example1.txt

Here is an example1 of some text. The program

will read this example1 text, update every

occurrence of example1 to append the given

number, N, then write the modified file to

%26lt;filename%26gt;N.txt.





#endif

No comments:

Post a Comment