#### Archived

This topic is now archived and is closed to further replies.

# Adding strings to text files..

This topic is 6105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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."

##### Share on other sites
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

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. #### Share this post ##### Link to post ##### Share on other sites Or do it with streams ...   #include #include 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;}  #### Share this post ##### Link to post ##### Share on other sites 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. #### Share this post ##### Link to post ##### Share on other sites I really must read the orignal posts more closely #### Share this post ##### Link to post ##### Share on other sites Sorry for the delay, had to go to a meeting. Hope this is better ...   #include #include 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;}  #### Share this post ##### Link to post ##### Share on other sites 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)

##### Share on other sites
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

##### Share on other sites
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."

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002278
×