I have a txt file (inFile.txt) that has int_1 int_2 int_3 .... ... int_n where n is unknown. Is there anyway that I can replace value int_3 by (int_3-100) without using a temporary file? Any hint/help would be appreciated.
Because my infile.txt is too big and I also need to copy and rename infile.txt as outfile.txt, I use fgets function to get line by line and use the "sscanf" to assign the value and change the variable as well as its value.
There is many ways to do what you want and I think the simple is do it whith a CString cause it facilitate the search. Here is ,at the glance what could it be like.
I hope it will be helpfull.
Suppose you have opened your textfile Using a CFile file
CString str;
LPSTR wherToRead=str.GetBuffer(file.GetLength()); // define memory where to read the file
file.Read(whereToRead,file.GetLength()); // reading the file
str.ReleaseBuffer();
// Do what you want to do with the file
// And now the search
int startSearch=0; // where to start the search , first from the begining 0
int positionFound=0; // The position of the found occurence
while(1)
{
positionFound=str.Find("int_",startSearch);
if(positionFound==-1) break; // if not found go out
str.Insert(positionFound+5,"-100" // insert what you want at the position found + number of the occurence here int_x=5 characters modify it as you like
startSearch=positionFound+5;
}
// now writing the new file
LPSTR fromToWrite=str.GetBuffer(str.GetLength());
file.Write(fromToWrite,str.GetLength());
str.ReleaseBuffer();
.........
c.ami@caramail.com
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.