Board index » cppbuilder » why FileWrite faild to write all string to disk?

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
 
 

Re:why FileWrite faild to write all string to disk?

"julius" < XXXX@XXXXX.COM >wrote in message
Quote
this->HandleOfCurrentHTAFile = FileCreate(strHTAFile);
FileCreate() will overwrite the file if it already exists. Is that what you
want?
Quote
WrittenLen =
FileWrite(HandleOfCurrentHTAFile,strMsg.c_str(),strMsg.Length());
Where are you assigning str? Does the value of WrittenLen match the value
of str.Length()?
Your description is much too vague. You need to provide more details.
Gambit
 

Re:why FileWrite faild to write all string to disk?

"julius" < XXXX@XXXXX.COM >wrote in message
Quote
if (CurrentHTAFile>0)
You should be checking for -1, not 0. 0 May be a valid file handle value.
Quote
BytesWritten =
FileWrite(CurrentHTAFile,strMsg.c_str(),strMsg.Length());
ErrorNumber=GetLastError();
if (BytesWritten!=strMsg.Length())
{
ErrorNumber=GetLastError();
}
One thing to take into account is that BytesWritten may not always equal
strMsg.Length(), but that does not necessarily mean that an error occured.
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}

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" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>this->HandleOfCurrentHTAFile = FileCreate(strHTAFile);

FileCreate() will overwrite the file if it already exists. Is that what
you
want?

>WrittenLen =
FileWrite(HandleOfCurrentHTAFile,strMsg.c_str(),strMsg.Length());

Where are you assigning str? Does the value of WrittenLen match the value
of str.Length()?

Your description is much too vague. You need to provide more details.


Gambit


 

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 >写入邮件
Quote
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 >写入邮件
news: XXXX@XXXXX.COM ...
>
>"julius" < XXXX@XXXXX.COM >wrote in message
>news: XXXX@XXXXX.COM ...
>
>>this->HandleOfCurrentHTAFile = FileCreate(strHTAFile);
>
>FileCreate() will overwrite the file if it already exists. Is that what
you
>want?
>
>>WrittenLen =
>FileWrite(HandleOfCurrentHTAFile,strMsg.c_str(),strMsg.Length());
>
>Where are you assigning str? Does the value of WrittenLen match the
value
>of str.Length()?
>
>Your description is much too vague. You need to provide more details.
>
>
>Gambit
>
>