• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

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
0
SumitKalsait
Asked:
SumitKalsait
  • 3
1 Solution
 
alb66Commented:
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 )
      {
      }
0
 
alb66Commented:
For reading:


      CStdioFile oFile;
      if ( oFile.Open( strFileIndex, CFile::modeRead|CFile::shareDenyWrite ))
      {
            CString str;
            if ( oFileIndex.ReadString( str ))
            {
                  ...
0
 
SumitKalsaitAuthor Commented:
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
0
 
alb66Commented:
>>>> 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()
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now