The MFC Professional

NEWBIE TIP #9
File I/O with MFC


This tip comes from a couple of posts in comp.os.ms-windows.programmer.tools.mfc...


Hi! I'm an MFC newbie and I've been struggling with the file I/O aspects of C++ / MFC. I want to just read in (and write out) a text file, but when I use ClassWizard, it seems to want to stick some kind of versioning information in the I/O stream (like its "typing" each kind of file). Can anyone point to an example, or give me a clue on the right way to implement the file i/o? Thanks!
If you just want to read/write a file, then you can do it with CFile. For example: CFile myFile; char szBuffer[40]; // put some data in c:\myfile.dat myFile.Open("c:\\myfile.dat", CFile::modeCreate); lstrcpy(szBuffer, "put this in the file"); myFile.Write(szBuffer, lstrlen(szBuffer)); myFile.Close(); // read from c:\myfile.dat myFile.Open("c:\\myfile.dat", CFile::modeRead); myFile.Read(szBuffer, sizeof(szBuffer)); myFile.Close(); Abu Wawda wawda@scf.usc.edu
> Try CStdioFile. It allows you to use very simple functions like > ReadString() and WriteString() to do file IO. Fantastic! I have learned something new! God I love this.... I have been using the fopen and fgets for so long, and now I can drop them. Hmmmmm, maybe looking at the manuals would be a good thing......

| Past Tips | Future Tips | Home |

Comments on this Tip? Send us mail.
(Be sure to put the Tip # in the subject.)

Got an idea for a Tip you'd like us to write? Let us know.

Solved a problem lately that would make a good Tip? PLEASE tell us about it!!!
We'll give you prominent credit, and we'll even buy you a beer next time you're in town.