Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Questions about C++ Builder and File I/O

Reply
Thread Tools

Questions about C++ Builder and File I/O

 
 
reyalp
Guest
Posts: n/a
 
      02-02-2007
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[i] << " ";
outfile << endl;
}
//---------------------------------------------------------------------------

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

}
//---------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Dennis Jones
Guest
Posts: n/a
 
      02-02-2007

"reyalp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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


 
Reply With Quote
 
 
 
 
Markus Svilans
Guest
Posts: n/a
 
      02-02-2007
On Feb 1, 10:42 pm, "reyalp" <(E-Mail Removed)> wrote:
> 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[i] << " ";
> 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:fstream
constructor, because it will open in output mode by default.

Regards,
Markus.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where's a DOM builder that uses the Builder Pattern to ... buildDOMs? Phlip Python 5 01-13-2010 12:48 PM
Java Swing jCheckbox creation from an external file, and not using a "GUI Builder"?? henderjm@gmail.com Java 3 08-04-2006 02:07 AM
Web Development from C++ Builder Version 6.0 basic questions. quincy451@yahoo.com C++ 1 01-07-2005 11:58 PM
Ruby Cocoa (OS X) questions: deployment & interface builder Michael DeHaan Ruby 11 12-23-2004 02:55 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments