Questions about C++ Builder and File I/O

R

reyalp

I use C++ Builder to create a simple project that can open a picture
and save some text information to a file
My question is that:

After I execute the open picture dialog(btn_OpenPicture) and open the
picture in the form, the write to file function(btn_SaveClick) doesn't
work anymore. Every time I click the save button, nothing changed in
the file.
But before the execution of open picture, it works normally.
Why does that happen? thanks

My code:
void __fastcall TForm1::btn_SaveClick(TObject *Sender)
{
int i;
double num[49];
ofstream outfile("Data.txt", ios_base::app);
if (!outfile)
return;
i = 20;
outfile << i << endl;
for (i = 0; i < 49; i++)
outfile << num << " ";
outfile << endl;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::btn_OpenPictureClick(TObject *Sender)
{
OpenPictureDialog1->Execute();
Image1->Picture->LoadFromFile(OpenPictureDialog1-
FileName);
}
//---------------------------------------------------------------------------
 
D

Dennis Jones

reyalp said:
I use C++ Builder to create a simple project that can open a picture
and save some text information to a file

Your question would be more appropriate in:

borland.public.cppbuilder.vcl.components.using

at

forums.borland.com

- Dennis
 
M

Markus Svilans

I use C++ Builder to create a simple project that can open a picture
and save some text information to a file
My question is that:

After I execute the open picture dialog(btn_OpenPicture) and open the
picture in the form, the write to file function(btn_SaveClick) doesn't
work anymore. Every time I click the save button, nothing changed in
the file.
But before the execution of open picture, it works normally.
Why does that happen? thanks

My code:
void __fastcall TForm1::btn_SaveClick(TObject *Sender)
{
int i;
double num[49];
ofstream outfile("Data.txt", ios_base::app);
if (!outfile)
return;
i = 20;
outfile << i << endl;
for (i = 0; i < 49; i++)
outfile << num << " ";
outfile << endl;}


Hello,

In the btn_SaveClick() function, you are writing the uninitialized
array num[] to the file. This may explain why the file contents change
only the first time you click the button. When you click the button
again, the same uninitialized array is written, and if the memory
where the array resides has not changed, then the file contents will
not change either.

I suspect you may have some variable scope confusion. When you open
your image, do you process some data out of it, and store the data in
another num[] array that is declared in the scope of the TForm1 class?
If that is what's happened, then your locally declared num[] array
will be accessed, and this might not be what you want.

Also, the output file is opened in append mode (ios_base::app).
Perhaps you meant to open it in output mode? In output mode, the
existing file contents are deleted before new contents are written.
Then you can also omit the second parameter to the std::eek:fstream
constructor, because it will open in output mode by default.

Regards,
Markus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top