On Feb 1, 10:42 pm, "reyalp" <vict...@gmail.com> 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.