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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying contents of a .txt-file using MFC 1

Status
Not open for further replies.

d00ape

Programmer
Apr 2, 2003
171
SE
Hi!

If I have the following “C:\textfile.txt” with contents:
---------------
Hi there.
This dog is blue and his ID: 1234. BlaBla…
---------------

and would traverse it and find “ID:” and change “1234” to for e.g. “56” so the file would look like:
---------------
Hi there.
This dog is blue and his ID: 45. BlaBla…
---------------

I’m programming VC++ and MFC. Any tips of how I could tackle my problem.

Thanks - Anders
 
A bit confused about what you wanted to replace it with, but assuming you want you replace the "1234" with "56" this could work:
Code:
const CString cFileName = "C:\\textfile.txt";
CString s;
{
	CFile f(cFileName,CFile::modeRead);
	UINT len=f.GetLength();
	f.Read(s.GetBuffer(len),len);
	s.ReleaseBuffer();
}
s.Replace("ID: 1234","ID: 56");
{
	CFile f(cFileName,CFile::modeWrite);
	f.Write((LPCTSTR)s,s.GetLength());
}

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top