Board index » cppbuilder » why FileWrite faild to write all string to disk?
julius
CBuilder Developer |
julius
CBuilder Developer |
why FileWrite faild to write all string to disk?2005-04-13 10:32:47 PM cppbuilder26 Hello,Expert: I try to write some info to disk with following file handlers, but not all records are correctly written to disk. some records are missing. this->HandleOfCurrentHTAFile = FileCreate(strHTAFile); WrittenLen = FileWrite(HandleOfCurrentHTAFile,strMsg.c_str(),strMsg.Length()); FileClose(HandleOfCurrentHTAFile); The IDE is C++BUILDER 6.0 Thanks Julius Wang |
Remy Lebeau (TeamB)
CBuilder Developer |
2005-04-14 12:59:04 AM
Re:why FileWrite faild to write all string to disk?
"julius" < XXXX@XXXXX.COM >wrote in message
Quotethis->HandleOfCurrentHTAFile = FileCreate(strHTAFile); QuoteWrittenLen = of str.Length()? Your description is much too vague. You need to provide more details. Gambit |
Remy Lebeau (TeamB)
CBuilder Developer |
2005-04-15 06:12:22 AM
Re:why FileWrite faild to write all string to disk?
"julius" < XXXX@XXXXX.COM >wrote in message
Quoteif (CurrentHTAFile>0) QuoteBytesWritten = BytesWritten may be>0, meaning that some but not all bytes were written, and you then need to call WriteFile() again to write the rest of the bytes. So, try putting FileWrite() into a loop until all known bytes have been written in full. For example: void __fastcall TForm1::WriteHTARecord(AnsiString strMsg) { int CurrentHTAFile = -1; int BytesWritten = 0; int ErrorNumber = 0; if( WorkingFile !="" ) { if( FileExists(WorkingFile) ) { CurrentHTAFile = FileOpen(WorkingFile, fmOpenReadWrite | fmShareDenyWrite); if( CurrentHTAFile != -1 ) FileSeek(CurrentHTAFile, 0, 2); } else CurrentHTAFile = FileCreate(WorkingFile); if( CurrentHTAFile != -1 ) { int Len = strMsg.Length(); char *Data = strMsg.c_str(); while( Len>0 ) { BytesWritten = FileWrite(CurrentHTAFile, Data, Len); if( BytesWritten < 1 ) { ErrorNumber = GetLastError(); break; } Data += BytesWritten; Len -= BytesWritten; } FileClose(CurrentHTAFile); } } } Gambit {smallsort} |
julius
CBuilder Developer |
2005-04-15 02:58:33 PM
Re:why FileWrite faild to write all string to disk?
Hello,thanks for your reply.
Below is the source code I write: I use it to write one record of a file, but the strange thing is that, it fails to the last record of some files,but some other files are correctly written, when I write 10 files consequently. And the number of bytes written is always equal to the length of string. void __fastcall TForm1::WriteHTARecord(AnsiString strMsg) { int CurrentHTAFile =-1; int BytesWritten=0; int ErrorNumber=0; if (this->WorkingFile!="") { CurrentHTAFile = FileOpen(this->WorkingFile, fmOpenReadWrite); } if (CurrentHTAFile>0) { //file exist, so write this record FileSeek(CurrentHTAFile,0, 2); BytesWritten = FileWrite(CurrentHTAFile,strMsg.c_str(),strMsg.Length()); ErrorNumber=GetLastError(); if (BytesWritten!=strMsg.Length()) { ErrorNumber=GetLastError(); } FileClose(CurrentHTAFile); } else {//File does not exist, create it and write record to disk CurrentHTAFile = FileCreate(this->WorkingFile); if (CurrentHTAFile>0) { BytesWritten=FileWrite(CurrentHTAFile,strMsg.c_str(),strMsg.Length()); ErrorNumber=GetLastError(); if (BytesWritten!=strMsg.Length()) { ErrorNumber=GetLastError(); } FileClose(CurrentHTAFile); } } } "Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >写入邮件 Quote
|
julius
CBuilder Developer |
2005-04-15 04:06:25 PM
Re:why FileWrite faild to write all string to disk?
Hello:
I have found the reason. It is because of Socket buffer full, not because of file handling functions. Sorry for disturbance and thanks to your reply. Best Regards Julius Wang "julius" < XXXX@XXXXX.COM >写入邮件 QuoteHello,thanks for your reply. |