How to reading and writting string values from Formatted CFile ?

Posted on 2009-02-24
Last Modified: 2013-11-20
Hello I have to read or write string file in txt file like

Product name : Bla bla bla

Product ID : 12345

Product available : yes

Product purchase date: 12/3/09

if i like to change something like this

Product name : AA AA AA

How can i do it with CFile?

Thanks for the Help
Question by:SumitKalsait
    LVL 19

    Expert Comment

    Since you need a text file you can use CStdioFile instead of CFile.

                CString str;

                CStdioFile oFile( "test.txt", CFile::modeCreate|CFile::modeWrite );

                str.Format( "Product name : %s\n", "Bla Bla Bla" );
                oFile.WriteString( str );

                str.Format( "Product ID : %d\n", 12345 );
                oFile.WriteString( str );

          catch( CFileException )
    LVL 19

    Expert Comment

    For reading:

          CStdioFile oFile;
          if ( oFile.Open( strFileIndex, CFile::modeRead|CFile::shareDenyWrite ))
                CString str;
                if ( oFileIndex.ReadString( str ))

    Author Comment

    but how to write at perticular position how to find the string that you want to edit, actually
    str.Format( "Product ID : %d\n", 12345 );
                oFile.WriteString( str );

    replace whole content of the file with this written string thanks
    LVL 19

    Accepted Solution

    >>>> replace whole content of the file with this written string thanks
    That is because CFile::modeCreate flag is used.
    If you want to modify an existing file remove this flag.

    >>>> how to write at particular position
    You can use Seek() to move to a specific offset in the file
    oFile.Seek( nNewOffset, CFile::begin );

    >>>> how to find the string that you want to edit
    You can read the whole content of the file in a memory buffer with CFile::Read()
    and then searching for the string using strstr()

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    function returning CString in a regular win32 DLL 8 133
    Perl Awk Need Help 3 85
    mapAB Challlenge 35 50
    wordappend challenge 8 47
    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now