Questions about C++ Builder and File I/O

Discussion in 'C++' started by reyalp, Feb 2, 2007.

  1. reyalp

    reyalp Guest

    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);

    }
    //---------------------------------------------------------------------------
    reyalp, Feb 2, 2007
    #1
    1. Advertising

  2. reyalp

    Dennis Jones Guest

    "reyalp" <> wrote in message
    news:...
    >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
    Dennis Jones, Feb 2, 2007
    #2
    1. Advertising

  3. On Feb 1, 10:42 pm, "reyalp" <> 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 << " ";
    > 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.
    Markus Svilans, Feb 2, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elhanan
    Replies:
    3
    Views:
    11,223
    Jean-Francois Briere
    Dec 17, 2005
  2. Replies:
    1
    Views:
    360
    Mike Wahler
    Jan 7, 2005
  3. Replies:
    3
    Views:
    472
    Andrew Thompson
    Aug 4, 2006
  4. Phlip
    Replies:
    5
    Views:
    551
    Stefan Behnel
    Jan 13, 2010
  5. Michael DeHaan
    Replies:
    11
    Views:
    245
Loading...

Share This Page