Trou
asked on
Get trouble while using TFileStream.
here is a piece of my program for save information to file.
void object::WriteToFile(TFileS ream *pFile)
{
...
pFile->Position=2;
pFile->Write(Buffer,Num);/ /int* Buffer
pFile->Position=2;//for debug
pFile->Read(Buffer,Num);// for debug
return;
}
Statement 3,4 are for debuging consistent between content of buffer before storing and those after storing.
function WriteToFile are called by handler FileSaveExecute() of a menu item.a TFileStream object is created inside FileSaveExecute() useing new,and delete before return from FileSaveExecute().But the resulting file after called FileSaveExecute() is incorrently(not consistent),
while consistent is got in the debuging.Buffer[1],Buffer[ 2]....(Num >0)are always 0 in the resulting file except for Buffer[0] which is stored corrently.what wrong with my program?
i plan to use CArchive which is a MFC class instead of TFileStream.Does C++Builder mind?
the version of C++builder is 5.0
void object::WriteToFile(TFileS
{
...
pFile->Position=2;
pFile->Write(Buffer,Num);/
pFile->Position=2;//for debug
pFile->Read(Buffer,Num);//
return;
}
Statement 3,4 are for debuging consistent between content of buffer before storing and those after storing.
function WriteToFile are called by handler FileSaveExecute() of a menu item.a TFileStream object is created inside FileSaveExecute() useing new,and delete before return from FileSaveExecute().But the resulting file after called FileSaveExecute() is incorrently(not consistent),
while consistent is got in the debuging.Buffer[1],Buffer[
i plan to use CArchive which is a MFC class instead of TFileStream.Does C++Builder mind?
the version of C++builder is 5.0
ASKER
i now use TFileStream::Seek() to set position instead of setting position
property directly.But the program still runs incorrently with m_pFile->
Write(Buffer,Num).in the resulting file, only the frist item of Buffer[Num] are
stored corrently,and anyother item are stored as 0.
i also try to use the following statement intead of m_pFile->Write(Buffer,Num) ;
for(int Timer=0;Timer<Num;Timer++) m_pFile->W rite(&Buff er[Timer], 1);
and get a resulting file of corrent!
i have no idea now.By the way Buffer[Num] is created using operator new.
property directly.But the program still runs incorrently with m_pFile->
Write(Buffer,Num).in the resulting file, only the frist item of Buffer[Num] are
stored corrently,and anyother item are stored as 0.
i also try to use the following statement intead of m_pFile->Write(Buffer,Num)
for(int Timer=0;Timer<Num;Timer++)
and get a resulting file of corrent!
i have no idea now.By the way Buffer[Num] is created using operator new.
"for(int Timer=0;Timer<Num;Timer++) m_pFile->W rite(&Buff er[Timer], 1);"
-This gives you an INCREMENTAL write to file.
for(int Timer=0;Timer<Num;Timer++) {
m_PFile->Position=2;
m_pFile->Write(&Buffer[Tim er],1);
}
-This OVERWRITES the beginning of the stream.
-Check your code that you are looping when you try the Seek or set the Position property. My guess is that is where your problem lies.
Good luck!!
-This gives you an INCREMENTAL write to file.
for(int Timer=0;Timer<Num;Timer++)
m_PFile->Position=2;
m_pFile->Write(&Buffer[Tim
}
-This OVERWRITES the beginning of the stream.
-Check your code that you are looping when you try the Seek or set the Position property. My guess is that is where your problem lies.
Good luck!!
ASKER
OK.i have found out the bug.the secord paramater of TFileStream::Write(void*,i nt Count)
,Count,is Number of BYTE.And buffer is of type int,for int is 32-bit.The bug in my program is
write 4 int-variables to file with paramater Count=4.so only the frist variable can be wrote to
file.
finally bless you.
,Count,is Number of BYTE.And buffer is of type int,for int is 32-bit.The bug in my program is
write 4 int-variables to file with paramater Count=4.so only the frist variable can be wrote to
file.
finally bless you.
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity, I will suggest to refund the points and PAQ at zero points since you found your own solution.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good luck!!