Virtual Developer Workshop: Containerized Development with Docker

Environment: VC++ 6

Reading and Writing Files Directly

MFC's and App Wizard's built-in serialization capabilities are great if what you are doing is within defined parameters such as stream I/O. But what about an old-fashioned, powerful way of handling files like non-Windows programmers: creating, reading, and writing files directly? There is some nifty code on Code Guru that addresses this issue via custom functions; however, there is a simple, old-fashioned way of handling file I/O at a lower level, and it's VERY powerful!

The MFC Library has the CFile class. Here are the Member Functions of the CFile Class:

Member Function Description
CFile Creates the CFile object. If passed a filename, it opens the file.
Destructor Cleans up a CFile object that's going out of scope. If the file is open, it closes that file.
Abort() Immediately closes the file with no regard for errors.
Close() Closes the file.
Duplicate() Creates a duplicate file object.
Flush() Flushes data from the stream.
GetFileName() Gets the file's filename.
GetFilePath() Gets the file's full path.
GetFileTitle() Gets the file's title (the filename without the extension).
GetLength() Gets the file's length.
GetPosition() Gets the current position within the file.
GetStatus() Gets the file's status.
LockRange() Locks a portion of the file.
Open() Opens the file.
Read() Reads data from the file.
Remove() Deletes a file.
Rename() Renames the file.
Seek() Sets the position within the file.
SeekToBegin() Sets the position to the beginning of the file.
SeekToEnd() Sets the position to the end of the file.
SetFilePath() Sets the file's path.
SetLength() Sets the file's length.
SetStatus() Sets the file's status.
UnlockRange() Unlocks a portion of the file.
Write() Writes data to the file.

As you can see, the CFile class offers real file-handling power. Here is some practical sample code to demonstrate the use of the CFile class. This creates and opens a file, and writes a string to it:

    // Create the file to Write to.
    CFile file("TESTFILE.TXT", CFile::modeCreate | 
    // Write data to the file.
    CString message("Hello World!");
    int length = message.GetLength();
    file.Write((LPCTSTR)message, length);

Reading from a file isn't much different than writing to one:

    // Open the file to Read from.
    CFile file("TESTFILE.TXT", CFile::modeRead);
    // Read data from the file.
    char s[81];
    int bytesRead = file.Read(s, 80);
    s[bytesRead] = 0;
    CString message = s;

These examples use hard-coded filenames. For a more pleasurable user experience, and to add a nice touch to your programs, use the MFC class CFileDialog (in the online help).


  • Juz wat i need

    Posted by Legacy on 07/20/2003 07:00am

    Originally posted by: Ks

    Juz started on MFC and finally found some thing simple i can use for file...instead of all the documenting on OBDC...etc. Thanx Man!

  • read lines

    Posted by Legacy on 04/16/2003 07:00am

    Originally posted by: ozzy777

    i need to creat a routine that reads data from files, but the files may have different numbers of variables per line.
    i tried with "sscanf" but you need to specify the variables it should read

  • Sorry

    Posted by Legacy on 04/03/2003 08:00am

    Originally posted by: MoBo

    Me becomes still bad, if I see this "beautiful" code.
    Thats code for prebeginners or postprebeginners.
    Why you not take simply MFC - CStdioFile. This class has members ReadString and WriteString. VERY Powerfull !!! ;-)

  • Is there a way to store and retrieve to/from a file a record structure?

    Posted by Legacy on 11/27/2002 08:00am

    Originally posted by: Gain

    Is there a way to store and retrieve to/from a file a record structure? Thanks.

  • Additional info needed

    Posted by Legacy on 11/18/2002 08:00am

    Originally posted by: marco


    I found the example useful. thank you.
    Can you please tell me how I would add to this code
    to read through an unknown amount of records in a text file.

    Essentially I am looking for a way to:
    1 - read the first line
    2 - recognize the end of line
    3 - go to the next line and read it
    4 - continue this until i reach the end of file.

    Thank you

  • Whats that

    Posted by Legacy on 08/20/2002 07:00am

    Originally posted by: MoBo

    Who needs this code ?
    It's to simple. I think there is no need to put it to codeGURU. What, if I need to save 2 strings with variable length. No, I not realy need a solution for this. I only show You that Your sample is to simple for a realy programmer.
    (sorry for english, but I think it is understandable)

  • Deprecation

    Posted by Legacy on 08/20/2002 07:00am

    Originally posted by: Bambilici cu tarlici

    Why do you claim it works with vc6 when it works with vc 1.52 ?

  • This should have been reviewed before posting

    Posted by Legacy on 08/20/2002 07:00am

    Originally posted by: njkayaker

    Sorry, the original posting is pretty lame and not really that informative. It's also somewhat misleading since it implies that CFile is not a part of MFC and is portable.

    If the posting had more detail, a discussion of even such a "basic" thing as CFile might be useful.

    I've gotten a great deal of value from this site (and have made some modest "positive" contributions.

    The way this site will remain useful is if only high quality stuff (basic or advanced) gets posted.

  • Unbelievable

    Posted by Legacy on 08/20/2002 07:00am

    Originally posted by: Mark

    It never ceases to amaze me that developers that use this site fail to understand why it is here. It is here to help people, of all levels, not just the so-called *gurus*.

    The remarks by some of those gurus posted prior to this show a complete lack of understanding and tolerance and a large degree of childishness. Perhaps it is their comments that should be pulled.

    MoBo and Relayer should also understand that not everyone is at their heightened level of existence and may require more help than others.

    Rather than criticising perhaps they should have the guts to post some of their own code.

  • exquisit

    Posted by Legacy on 08/20/2002 07:00am

    Originally posted by: Nosferatu

    This article really lears visual Basic programmers how to handle IO. Cool... The programming world is full of unexpected and wonderful things for ASPists and MFCsts...
    What do you know? There are functions like fread, fscanf, fopen and so forth upon which the whole MFC is actually built, not to mention C++.
    But as someone said, this code might be an eyes opener. But, I would remove the "old fashioned way" from the title and call it "low level"

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date