This is code
void func(CString string)
{
pFile.Open(m_FilePath, CFile::modeWrite | CFile::modeCreate);
pFile.Seek( 0, CFile::end );
if ( pFile.GetLength() > (m_iFileSize) * 1024 * 1024 )//when file have some size{
pFile.Close(); //close a fileDeleteFile( m_FilePath + CString(".old") );//delete old file
MoveFile( m_FilePath, m_FilePath + CString(".old"));//move information from main file to old
if(!pFile.Open( m_FilePath, CFile::modeWrite | CFile::modeCreate))return;//open file again and write from begin
}
//here i write some information in file
pFile.Close();//close file
//end function
}
in this code MSVS always thow CFileExeption error i can`t understand what problem, i think problem inside IF operator, but i can find. Please help me with this problem.

Help! problem with CFile class
krig
void
CLog::AddString(LPCTSTR string ){
if
( pFile.Open( m_cLogFilePath, CFile::modeReadWrite ) ){
pFile.Seek( 0, CFile::begin);
ULONGLONG lenght = pFile.GetLength();
ULONGLONG size = m_iFileSize;
if ( lenght > ( ( size / 2 ) * 1024 * 1024 ) ){
pFile.Close();
DeleteFile( m_cLogFilePath + CString(".old") );
MoveFile( m_cLogFilePath, m_cLogFilePath + CString(".old") );
if ( ! pFile.Open( m_cLogFilePath, CFile::modeWrite|CFile::modeCreate ) ) return;}
else{
pFile.Seek( 0, CFile::end );
}
}
else{
if ( ! pFile.Open( m_cLogFilePath, CFile::modeWrite|CFile::modeCreate ) ) return;WORD nByteOrder = 0xFEFF;
pFile.Write( &nByteOrder, 2 );
}
CTime pNow = CTime::GetCurrentTime();
CString strLine;
strLine.Format( _T("-Date:|%.2i:%.2i:%.2i|->Event:<%s>\r\n"),pNow.GetHour(), pNow.GetMinute(), pNow.GetSecond(), string );
pFile.Write( (LPCTSTR)strLine,
sizeof(TCHAR) * strLine.GetLength() );pFile.Close();
}
what wrong in this code this code calls every function and save in file, if file have some size i close pFile and move this file to another and open again than write again from begin. WHAT I DO WRONG
muelle60
Hi
I think this code has many mistakes.
First why you called CFile::Open() with "CFile::modeCreate" parameter, so if the file is existed the file will be truncate to 0 length, so data will be deleted.
this instruction "pFile.Seek( 0, CFile::end )" is meaningless, so it has no effect.
If you want to determine the file has data or not you can detect if "CFile::GetLength()" is more than zero or not.
Best Regards.
barkingdog
Brandon Patram
Hi
I think you need function like this:
void
func(CString source_File, CString Dest_File){
CFile sourceFile;
CFile destFile;
// open the source file for reading if (!sourceFile.Open(source_File,CFile::modeRead)){
return;}
else{
if (!destFile.Open(Dest_File,CFile::modeWrite|CFile::modeCreate)){
sourceFile.Close();
return;}
ULONGLONG someValue = 1024;
if (sourceFile.GetLength() > someValue){
BYTE buffer[4096];
DWORD dwRead;
// Read in 4096-byte blocks, // remember how many bytes were actually read, // and try to write that many out. This loop ends // when there are no more bytes to read. do{
dwRead = sourceFile.Read(buffer, 4096);
destFile.Write(buffer, dwRead);
}
while (dwRead > 0);}
// Close both filesdestFile.Close();
sourceFile.Close();
}
}
You can delete the file at the end of this function or as you prefer.
Regards.
cassiobaurutil
this is good idea but your code slow i have localize problem she in this part of code
pFile.Close();//close opened file
DeleteFile( _T("filename2.txt"));//delete file number 2
MoveFile( _T("filename.txt"), _T("filename2.txt"));//move file number 1 to file number 2
if
( !pFile.Open( m_cLogFilePath, CFile::modeWrite|CFile::modeCreate ) )//open file number 1 - return if failed return;please help!!!!!!!!!
Gurpreet Singh Gill
The code is strange. You specify modeCreate, and then you try to call Seek and later GetLength. What exactly are you trying to do here
Ashish Garg
What's the value of m_iFileSize
In this case the file length will be zero always. So GetLength function will always return a zero. The if block executes only when m_iFileSize <= 0
Then the other problem is you are not checked the return value of Open function call of CFile Object. What I assume is that the file not opened properly. that's why the exception occurs on calling the other functions like GetLength and Close.
It seems that the exception coming from the GetLength function.
Move it out from the if statement. If you put it inside the if statement, it will be hard to know from where the exception occurs.
Get it to a variable like
int nFileLength = pFile.GetLength();
if ( nFileLength > (m_iFileSize) * 1024 * 1024 )//when file have some size
Now check from where the exception coming. To know from where the exception occurs. please check the call stack while debugging. Also check the error code (if any) on opening the file (put @err in watch window).
I have one more suggestion is that, when writing lengthy conditions in the if block, please give appropriate parenthesis, even if we know the precedence of operators.
e.g if ( nFileLength > (m_iFileSize * 1024 * 1024 ))
The above code will be easier to understand and chance for occurring error is less. Anyway this is just a suggestion. it's depends on you.
weiran
if ( lenght > ( ( size / 2 ) * 1024 * 1024 ) )