Archived

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

Chronoslade

Adding strings to text files..

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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 <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;
}

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
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;
}

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 this post


Link to post
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 this post


Link to post
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."

Share this post


Link to post
Share on other sites
    
#include <stdio.h>

#include <malloc.h>


#define ADD_WORD "WORD"

int main()
{
FILE *f1, *f2;
char word[128], c;

f1=fopen("infile","r");
f2=fopen("outfile","w");
while (!feof(f1)) {
fscanf(f1,"%s",word);
fprintf(f2,"%s%s",word,ADD_WORD);
c=fgetc(f1);
if (c == '\n')
fprintf(f2,"\n");
else if (c == ' ')
fprintf(f2," ");
}
fclose(f1); fclose(f2);
return 0;
}



Edited by - Maximus on November 2, 2001 11:59:02 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Stoffel
Just kidding. You''ve got mad regexp skills.


Not really. For one thing, I got it wrong...

sed -e "s/[^ \n\t\v\f]+/\1$STRING/g" < in.txt > out.txt

And Arild, not it isn''t: http://unxutils.sourceforge.net/

For reasons that are unclear, ''+'' appears to be broken in that version of sed, so you''d have to use:

sed -e "s/[^ \n\t\v\f][^ \n\t\v\f]*/\0$STRING/g" < in.txt > out.txt

All your bases belong to us

Share this post


Link to post
Share on other sites