hello, my head has been spinning around a way to do this. the best way to describe it would be with psudo code so...

ReplaceLine (string file, string keyword, string str) {
open file "file"
search for first line containing keyword "keyword"
replace that entire line with string "str"
save file "file"

return 0
}

very undetailed but, deliberately so. for instance for efficiency would you read in the entire file or using while(!file.eof()) go through line by line.
presumably the line by line search. but the replacing part is what is making me stuck.

anyway, thanks alot
Dead.Rabit

Read the file line-by-line. Do not use eof() because it dosn't work the way you would think.

string line;
fstream in("filename",ios::in);
while( getline(in,line) )
{
   // blabla
}

the problem though is in the replacing of the line...

with the nature of only being able to either read or write to a file, youd use ios::in to read each line to a string was as far as i got alone. but then storing which line you were in, switching to ios::out, deleting the line, and writing to that same line was the piece i was stuck on.

i could read in the entire file to say an array (more likely an STL container) of strings, replace the line that way, and then wipe the file and re-write to it. was the only completely viable method i could come up with

besides not sounding perticularly memory efficient, i dont know how to wipe a file, or indeed delete anything from a file.

every tutorial/lecture ive been to/seen seems to skim over the whole subject area so appologies. =]
thanks again
D.R

Use two different file pointers

ifstream in("filename"); // input file 
ofstream out("anotherfilename"); // output file
start of loop
    read a line
    check the string to see if it contains the search word
    if yes then write the new line to output file
    if no then just write the line to output file
end of loop

>>i dont know how to wipe a file, or indeed delete anything from a file.
See this code snippet. DaniWeb has a lot of C and C++ code snippets that I'll be you have not eveen seen.

This article has been dead for over six months. Start a new discussion instead.