Link to home
Start Free TrialLog in
Avatar of SumitKalsait
SumitKalsaitFlag for Germany

asked on

How to reading and writting string values from Formatted CFile ?

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
Avatar of alb66
alb66
Flag of Italy image

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

      try
      {
            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 )
      {
      }
For reading:


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

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of alb66
alb66
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial