I did not mean to criticize your program mate

I think fgetc() is just fine way to do that, when using the plain vanilla C

If one wishes to replace strings only when they're in one line, one can of course read the whole line to the memory for example with fgets(), and then examine it in a loop (like I did with my first desperate attempt, which may still be visible somewhere at OG... And that's terrible coding

) but I am not so sure it speeds up things too much
Nowadays I doubt I could do code like yours too quickly.. I would fell in every possible trap since I'm used to C++ and STL library it provides. I do not know about elegance, nor performance, but with C++ that would have been hel* of a lot easier to do
Errmm... I think I should not spam this thread... But I still want to tell why C++ eases this task so much..
1. strings. C++ alternative for char arrays (you can still use char arrays, but actually there's no need.)
- inbuilt memory allocation -> no overflows! : security + MUCH easier to use & avoid segfaults (You can ALWAYS do foo+="barbarbarbarbarbar..." or foo="barbarbarbarbar..." if your computer has enough free memory

Of course if you store foo="bar", you cannot read/assign value from/in foo[3] though.)
- overloaded operators like +, +=, and ==. (+to glue strings, or a string and a char array!, += glue and store in original string, == compare 2 strings)
-LOTS of functions to ease string handling. For example int find(string,[int position where to start search]) : see the previous code

erase(startingpoint,number of chars), substr(startingpoint,number of elements), length() etc...
2. vectors
like 2 dimensional arrays. You can do vector<string> foo, or vector<int> foo, or vector<whatever_even_your_own_object_type> foo. Works like arrays, but the memory allocation is once again inbuilt! you can always add new 'row' in vector, by simply using foo.push_back(what_to_push_back)
-similarly to string, vectors have bunch of functions to play with...
Now to demosnstrate the 'power' of strings, I'll show one way with pseudocode how to handle this with C++. Youll see MrG, that you do want to start using C++ too, especially when plain C is almost entirely included in C++
Code: Select all
1. read from file untill the end of file (for example line by line, if you use getline(foo,filestream) remember to add "\n" at the end of every line read, since getline does not store the "\n"), and store the results either in one large string, or in vector. Next do a loop to search the matchString
while( (position=(int)filecontents.find(matchString) )!=(int)string::npos)
{
//let's store the beginning of filecontent's that stays unchanged in string temp
temp=filecontents.substr(0,position);
//then we'll erase the whole beginning of filecontents, till the endo of the string we wished to replace.
filecontents.erase(position,matchstrlen);
//now let's append temp to contain the replacement and the rest of file.
temp+=replacementString+filecontents;
//finally store temp back in filecontents
filecontentes=temp;
}
Of course the abowe example does not work if we wish to replace "foo" with "hfoohjdl" for example. why?
That could be avoided for example by instead of storing the whole file back into the filecontents string, leaving the beginning of filecontent's string to be erased, and growing temp round by round to contain the whole file.
With vectors it would be almost similair, now we could just store each row in individual element of a vector, and examine the vector element by element. In this case it does not benefit us, but sometimes.. no often vectors are more than handy!