Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace

Status
Not open for further replies.

giahan

Programmer
Sep 1, 2000
139
US
Hi,

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.

Thanks
GH
 
If you read it all into a buffer made from CString it should work with the CString::Replace function.

CString str;
LPTSTR ptr = str.GetBuffer(LENGTH);
read the file into the buffer
str.ReleaseBuffer();
str.Replace("int_3","int_3-100);


That should do it.

Matt
 
Thanks Matt.

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.

GH
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top