CFile::SeekToEnd() 
Author Message
 CFile::SeekToEnd()

I can't seem to get this to work.  When I write a new string to the file, it
just writes over the previous file.  When i use trace to check the file
pointer after my SeekToEnd() line, it hasn't moved.  Is there something else
I have to do?  My code:

CString CMyProjectDoc::Log( CString szIn )
{
 //create CFile Object
 CFile fLog;
 char* pFileName = "Log.txt";//create pointe=r to the file name string
 CFileException eError;  //exception handler
 CString szError;
 char cBuf;

 if( !(fLog.Open( pFileName,  //File name
      CFile::modeCreate |  //create or open if it doesn't exist
      CFile::modeReadWrite |  //read/write access
      CFile::typeText,  //text file
      &eError ) ) )
 {
  szError.Format( "CFile::Open Error: %s." , eError.m_cause );
  AfxMessageBox( szError );
  fLog.Close();
  return eError.m_cause;
 }

 DWORD dwActual = fLog.SeekToEnd();  //go to end of file
 szError.Format( "File Pointer: %i.", dwActual );
 AfxMessageBox( szError );
 if( !dwActual && fLog.Read( &cBuf , 1 ) )
 {
  AfxMessageBox( "error" );
  return "error";
 }

 int nSend = szIn.GetLength();//length of string to send

 fLog.Write( szIn, nSend );//write string

 fLog.Close();//close file

 return szIn;

Quote:
}



Sat, 03 Feb 2001 03:00:00 GMT  
 CFile::SeekToEnd()
Thanks, I have another problem though, too.  For some reason, I can't get
the newline function to work with the typeText option either.  It all just
prints to one line with a "blob" in where the newline character should be?
Any thoughts?
Thanks for any help.

Ben

Quote:

>CFile::modeCreate causes the OS to truncate the existing file to 0 length
>when opened OR create a new file if it doesn't exist.

> Don't use this flag.



Sat, 03 Feb 2001 03:00:00 GMT  
 CFile::SeekToEnd()
CFile::modeCreate causes the OS to truncate the existing file to 0 length
when opened OR create a new file if it doesn't exist.

 Don't use this flag.



Sat, 03 Feb 2001 03:00:00 GMT  
 CFile::SeekToEnd()

This is because CFile only does binary i/o and thus doesn't expand \n
to \r\n as necessary.  Try using CStdioFile and CStdioFile::WriteString
instead.

HTH

: Thanks, I have another problem though, too.  For some reason, I can't get
: the newline function to work with the typeText option either.  It all just
: prints to one line with a "blob" in where the newline character should be?
: Any thoughts?
: Thanks for any help.
:
: Ben

:
:
: >CFile::modeCreate causes the OS to truncate the existing file to 0 length
: >when opened OR create a new file if it doesn't exist.
: >
: > Don't use this flag.
: >
: >
: >
: >
:
:

--
--
/*  Andrew  */
WWW: http://www.halcyon.com/ast        



Sat, 03 Feb 2001 03:00:00 GMT  
 CFile::SeekToEnd()

Quote:

> This is because CFile only does binary i/o and thus doesn't expand \n
> to \r\n as necessary.  Try using CStdioFile and CStdioFile::WriteString
> instead.

> HTH


> : Thanks, I have another problem though, too.  For some reason, I can't get
> : the newline function to work with the typeText option either.  It all just
> : prints to one line with a "blob" in where the newline character should be?
> : Any thoughts?
> : Thanks for any help.
> :
> : Ben

> :
> :

> : >CFile::modeCreate causes the OS to truncate the existing file to 0 length
> : >when opened OR create a new file if it doesn't exist.
> : >
> : > Don't use this flag.
> : >
> : >
> : >
> : >
> :
> :

> --
> --
> /*  Andrew  */
> WWW: http://www.halcyon.com/ast


Like this :

        if (!(fout.Open( filename, CFile::modeWrite | CFile::typeText, &e )))
            fout.Open( filename, CFile::modeCreate | CFile::modeWrite |
CFile::typeText, &e );        
        fout.SeekToEnd();
        fout.WriteString(line);
        fout.Close();



Sun, 04 Feb 2001 03:00:00 GMT  
 CFile::SeekToEnd()

Quote:

> I can't seem to get this to work.  When I write a new string to the file, it
> just writes over the previous file.  When i use trace to check the file
> pointer after my SeekToEnd() line, it hasn't moved.  Is there something else
> I have to do?  My code:

> CString CMyProjectDoc::Log( CString szIn )
> {
>  //create CFile Object
>  CFile fLog;
>  char* pFileName = "Log.txt";//create pointe=r to the file name string
>  CFileException eError;  //exception handler
>  CString szError;
>  char cBuf;

>  if( !(fLog.Open( pFileName,  //File name
>       CFile::modeCreate |  //create or open if it doesn't exist
>       CFile::modeReadWrite |  //read/write access
>       CFile::typeText,  //text file
>       &eError ) ) )
>  {
>   szError.Format( "CFile::Open Error: %s." , eError.m_cause );
>   AfxMessageBox( szError );
>   fLog.Close();
>   return eError.m_cause;
>  }

>  DWORD dwActual = fLog.SeekToEnd();  //go to end of file
>  szError.Format( "File Pointer: %i.", dwActual );
>  AfxMessageBox( szError );
>  if( !dwActual && fLog.Read( &cBuf , 1 ) )
>  {
>   AfxMessageBox( "error" );
>   return "error";
>  }

>  int nSend = szIn.GetLength();//length of string to send

>  fLog.Write( szIn, nSend );//write string

>  fLog.Close();//close file

>  return szIn;
> }

  Have you tried this :

 ui_open = f_logfile.Open(str_logname,CFile::modeCreate | CFile::modeNoTruncate
| CFile::modeWrite );
 if (ui_open)
      f_logfile.SeekToEnd();

It should work with \t and \n too.



Mon, 05 Feb 2001 03:00:00 GMT  
 CFile::SeekToEnd()
I don't know if I'm in the right stage of this thread, but I had the same
problem with SeekToEnd() ie it didn't.
I ended up using a work around.

Quote:


>> I can't seem to get this to work.  When I write a new string to the file,
it
>> just writes over the previous file.  When i use trace to check the file
>> pointer after my SeekToEnd() line, it hasn't moved.  Is there something
else
>> I have to do?  My code:

>> CString CMyProjectDoc::Log( CString szIn )
>> {
>>  //create CFile Object
>>  CFile fLog;
>>  char* pFileName = "Log.txt";//create pointe=r to the file name string
>>  CFileException eError;  //exception handler
>>  CString szError;
>>  char cBuf;

>>  if( !(fLog.Open( pFileName,  //File name
>>       CFile::modeCreate |  //create or open if it doesn't exist
>>       CFile::modeReadWrite |  //read/write access
>>       CFile::typeText,  //text file
>>       &eError ) ) )
>>  {
>>   szError.Format( "CFile::Open Error: %s." , eError.m_cause );
>>   AfxMessageBox( szError );
>>   fLog.Close();
>>   return eError.m_cause;
>>  }

>>  DWORD dwActual = fLog.SeekToEnd();  //go to end of file
>>  szError.Format( "File Pointer: %i.", dwActual );
>>  AfxMessageBox( szError );
>>  if( !dwActual && fLog.Read( &cBuf , 1 ) )
>>  {
>>   AfxMessageBox( "error" );
>>   return "error";
>>  }

>>  int nSend = szIn.GetLength();//length of string to send

>>  fLog.Write( szIn, nSend );//write string

>>  fLog.Close();//close file

>>  return szIn;
>> }

>  Have you tried this :

> ui_open = f_logfile.Open(str_logname,CFile::modeCreate |

CFile::modeNoTruncate

- Show quoted text -

Quote:
>| CFile::modeWrite );
> if (ui_open)
>      f_logfile.SeekToEnd();

>It should work with \t and \n too.



Sun, 18 Feb 2001 03:00:00 GMT  
 CFile::SeekToEnd()
I think here the problem is because U need a CFile::modeNoTruncate flag along
with CFile::Create Flag, else the file is truncated.

Rgds
Raji .S.

Quote:

> I don't know if I'm in the right stage of this thread, but I had the same
> problem with SeekToEnd() ie it didn't.
> I ended up using a work around.



> >> I can't seem to get this to work.  When I write a new string to the file,
> it
> >> just writes over the previous file.  When i use trace to check the file
> >> pointer after my SeekToEnd() line, it hasn't moved.  Is there something
> else
> >> I have to do?  My code:

> >> CString CMyProjectDoc::Log( CString szIn )
> >> {
> >>  //create CFile Object
> >>  CFile fLog;
> >>  char* pFileName = "Log.txt";//create pointe=r to the file name string
> >>  CFileException eError;  //exception handler
> >>  CString szError;
> >>  char cBuf;

> >>  if( !(fLog.Open( pFileName,  //File name
> >>       CFile::modeCreate |  //create or open if it doesn't exist
> >>       CFile::modeReadWrite |  //read/write access
> >>       CFile::typeText,  //text file
> >>       &eError ) ) )
> >>  {
> >>   szError.Format( "CFile::Open Error: %s." , eError.m_cause );
> >>   AfxMessageBox( szError );
> >>   fLog.Close();
> >>   return eError.m_cause;
> >>  }

> >>  DWORD dwActual = fLog.SeekToEnd();  //go to end of file
> >>  szError.Format( "File Pointer: %i.", dwActual );
> >>  AfxMessageBox( szError );
> >>  if( !dwActual && fLog.Read( &cBuf , 1 ) )
> >>  {
> >>   AfxMessageBox( "error" );
> >>   return "error";
> >>  }

> >>  int nSend = szIn.GetLength();//length of string to send

> >>  fLog.Write( szIn, nSend );//write string

> >>  fLog.Close();//close file

> >>  return szIn;
> >> }

> >  Have you tried this :

> > ui_open = f_logfile.Open(str_logname,CFile::modeCreate |
> CFile::modeNoTruncate
> >| CFile::modeWrite );
> > if (ui_open)
> >      f_logfile.SeekToEnd();

> >It should work with \t and \n too.



Sun, 18 Feb 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. CFile::SeekToEnd()

2. CFile::SeekToEnd()

3. CFile::SeekToEnd()

4. CFile::SeekToEnd()

5. CFile::Open asserts on CFile::typeText

6. CFile::Seek and CFile::Read over the network

7. Are CFile::Read() and CFile::Write() Really Unbuffered?

8. CFile error on the VC 7.0.

9. Inserting a string at the beginning of a file with CFile

10. CFile can't read file's update lie in novell

11. Is there a function similar to CFile?

12. CFile to CByteArray

 

 
Powered by phpBB® Forum Software