Solved

Get trouble while using TFileStream.

Posted on 2001-09-08
6
195 Views
Last Modified: 2008-02-26
here is a piece of my program for save information to file.
void object::WriteToFile(TFileSream *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
 
0
Comment
Question by:Trou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6467753
Try using the Seek method of TFileStream versus setting the Position property. The reason for this is that the linker may sometimes interpet the new Position value to mean "position relative to the current position", whereas Seek's second parameter(origin) tells it explicitly where to begin. Position, while on paper is a write/read property, should really only be used to read the current position from the beginning of the stream.


Good luck!!
 
0
 

Author Comment

by:Trou
ID: 6468122
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->Write(&Buffer[Timer],1);
and get a  resulting file of corrent!
i have no idea now.By the way Buffer[Num] is created using operator new.

0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6469100
"for(int Timer=0;Timer<Num;Timer++)m_pFile->Write(&Buffer[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[Timer],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!!

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Trou
ID: 6484659
OK.i have found out the bug.the secord paramater of TFileStream::Write(void*,int 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.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6865881
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
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 6886909
Per recommendation, points refunded and question closed by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question