Adding strings to text files..

Started by
11 comments, last by Chronoslade 22 years, 5 months ago
I have a text file with a bunch of strings contained in it. How do I add a specific string to the end of every word in the text file? "There is humor in everything depending on which prespective you look from."
"There is humor in everything depending on which prespective you look from."
Advertisement
If you can have 2 files, one input and one output, I''d read one word from the input file, add the string with either strcat() or sprintf() and then write it to the output file. To read the word, you could either read the whole file to memory and work through picking out words, or you could load it one character at a time into a buffer so only one word is in the buffer at a time.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

sed -e "s/[^ \n\t\v\f]+[^ \n\t\v\f]+/\\0$STRING/g" < inputfile > outputfile ?

Of course, if you''re using Windows, it''s trickier...

If you need to do it once, whilst creating the program, then most development suites will come with a sed, or a regexp replace facility (VC++ has a regular expression option in it''s Edit/Replace dialog)

Failing that, you''ll need a program.

    void appendToWord (char *string, FILE *fin, FILE *fout)  {    char word[256], *wp = word;    int c, bWhitespace = 0;    while (1) {       c = fgetc(fin);       if (c == EOF) {         return;       } else if (strchr(c, " \t\n\v\f")) {         // If the character is whitespace...         if (!bWhitespace)           // If the last character wasn''t whitespace, it''s the end of a word.           fprintf(fout, "%s", string);         fputc(fout, c);         // Now we''re in some whitespace. Stop the string being output.         bWhitespace = 1;       } else {         fputc(fout, c);         // This isn''t whitespace. We can output the string.         bWhitespace = 0;       }    }  }  


Call this with the string to append, the input file, and the output file. You might need to mess with it a little, since I''ve not tested it. I''m not sure I''ve got the arguments to fputc the right way ''round.

''Nuff said. I''ll enjoy watching you live, demon.
CoV
Or do it with streams ...

  #include <fstream>#include <string>void appendwordtofile(std::string theword, std::string in_file, std::string out_file){	const unsigned int storage_buf = 256;	char* storage = new char[storage_buf];	std::ofstream out_stream;	std::ifstream in_stream;	std::string output;	in_stream.open(in_file.c_str());	out_stream.open(out_file.c_str());	out_stream.clear();	while (!in_stream.eof())	{		in_stream.getline(storage, 256);		output = storage + theword + ''\n'';		out_stream.write(output.c_str(), output.length());			}	in_stream.close();	out_stream.close();	delete[] storage;}  
Chronoslade wants to add a string to each word. That function only adds to each line.

''Nuff said. I''ll enjoy watching you live, demon.
CoV
I really must read the orignal posts more closely
Sorry for the delay, had to go to a meeting.

Hope this is better ...

  #include <fstream>#include <string>void appendwordtofile(std::string theword, std::string in_file, std::string out_file){	const unsigned int storage_buf = 256;	char* storage = new char[storage_buf];	std::ofstream out_stream;	std::ifstream in_stream;	std::string output_fake;	std::string output;	in_stream.open(in_file.c_str());	out_stream.open(out_file.c_str());	out_stream.clear();	while (!in_stream.eof())	{		in_stream.getline(storage, 256);				output = storage;		int pos = output.find(" ", 0);				while (pos > 0)		{			output.insert(pos, theword);			pos = output.find(" ", (pos + theword.length() + 1));		}		output += theword + ''\n'';		out_stream.write(output.c_str(), output.length());			}	in_stream.close();	out_stream.close();	delete[] storage;}  
quote:
sed -e "s/[^ \n\t\v\f]+[^ \n\t\v\f]+/\\0$STRING/g" < inputfile > outputfile ?

Mayrel, no cussing allowed in the forums.

Just kidding. You''ve got mad regexp skills.

(learning Perl myself, but the going is slow)
quote:Original post by Mayrel
sed -e "s/[^ \n\t\v\f]+[^ \n\t\v\f]+/\\0$STRING/g" < inputfile > outputfile ?

Of course, if you''re using Windows, it''s trickier...


Why? Should hardly be a problem to find a win32 version of sed.



"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I need to learn to be more specific, I appreciate the help but I''m still haveing trouble little bit over my head.
My problem is I have a txt file with with strings seperated by whitespaces and newline, I need to read in this text file append a specific string to the end of every word and place the on seperate lines in a new text file. I looked at the one above and i dont really understand the first one and the streams are pretty much over my head. I''d like to do this in C if all possible but C++ will do also. Any help would be appreciated.



"There is humor in everything depending on which prespective you look from."
"There is humor in everything depending on which prespective you look from."

This topic is closed to new replies.

Advertisement