[Solved] How to Read and write into/from a text file?


This line

fprintf(l_pFile, sVar);

doesn’t look right. I think it should be

fprintf(l_pFile, "%s\n", (LPCTSTR) sVar);

The loop could become infinite if the file has less than two linefeeds:

while(count != 2)

I think it should be:

while( (count < 2) && ( ! feof(l_pFile) ) && ( c != EOF ) )

Probably unrelated to your error, but – at least for this code snippet – CString::ReleaseBuffer() doesn’t need to be called since you have not called CString::GetBuffer().

l_strRememberCheck.ReleaseBuffer();

This line may be unnecessary as it appears to fseek() to where the file pointer already is:

fseek(l_pFile,ftell(l_pFile),SEEK_SET);

In the event a two-line file is not terminated with a ‘\n’ you would need to print like this:

   if ( count == 2 )
   {
      fprintf(l_pFile, "%s\n", (LPCTSTR) sVar);
   }
   else
   {
      fprintf(l_pFile, "\n%s\n", (LPCTSTR) sVar);
   }

3

solved How to Read and write into/from a text file?