[Newbee] Problem with saving a file

Discussion in 'C++' started by Rene, Jul 30, 2008.

  1. Rene

    Rene Guest

    Hello to all!

    I am a newbee to C++, I did a beginners' course, and now I am stuck with a
    strange problem.

    I have written the function that is pasted downwards. The peculiar thing is
    that when I sent the function the value "0", the .lst file is created
    properly (the vector I am using/saving is made up of structs of one
    AnsiString (using BCB) called "naam" and one int called "nummer", I guess
    You'll get what they mean in English ;-)). When however I want to have the
    file backupped first to a .bak file, I sent it "1". In that case, the .bak
    file is created properly. But the .lst file not anymore, it remains empty. I
    have already stepped throught the code and the loop is executed in the
    normal way in the second part of the function when I enable the backup.
    However, the file on disk remains empty. Next time I call the function, the
    ..bak file is "filled" again but with emptiness which in itself is logical,
    there is nothing in the .lst file to be copied. However, when I call the
    function with "0" again, the .lst file again is filled like it is supposed
    to.

    I just can't figure out why the .lst file is not filled after the part of
    the function that makes the backup has been executed. BTW I am not using the
    files anywhere else in the program.

    Thank You very much in advance for any help!

    Yours sincerely,
    Rene

    P.S. "Bestand" is Dutch for "file".


    void TForm1::slaOp(bool backup)
    {
    fstream uitvoerBestand;
    fstream backupBestand;

    char letter;

    if (backup)
    {
    uitvoerBestand.open("Quizdata.lst", ios::in);
    backupBestand.open("Quizdata.bak", ios::eek:ut);

    uitvoerBestand.get(letter);
    while (!uitvoerBestand.eof())
    {
    backupBestand.put(letter);
    uitvoerBestand.get(letter);
    }
    backupBestand.put(letter);

    backupBestand.close();
    uitvoerBestand.close();
    }

    uitvoerBestand.open("Quizdata.lst", ios::eek:ut);

    for (int teller = 0; teller < aantalDeelnemers; teller++)
    {
    uitvoerBestand << setfill('0') << setw(3) << setprecision(3);
    uitvoerBestand << deelnemerLijst[teller].nummer;
    uitvoerBestand << deelnemerLijst[teller].naam.c_str();
    if (teller < aantalDeelnemers - 1)
    {
    uitvoerBestand << endl;
    }
    }
    uitvoerBestand.close();
    }
    Rene, Jul 30, 2008
    #1
    1. Advertising

  2. Rene

    Eric Pruneau Guest

    "Rene" <> a écrit dans le message de news:
    R_2kk.40$2...
    > Hello to all!
    >
    > I am a newbee to C++, I did a beginners' course, and now I am stuck with a
    > strange problem.
    >
    > I have written the function that is pasted downwards. The peculiar thing
    > is that when I sent the function the value "0", the .lst file is created
    > properly (the vector I am using/saving is made up of structs of one
    > AnsiString (using BCB) called "naam" and one int called "nummer", I guess
    > You'll get what they mean in English ;-)). When however I want to have the
    > file backupped first to a .bak file, I sent it "1". In that case, the .bak
    > file is created properly. But the .lst file not anymore, it remains empty.
    > I have already stepped throught the code and the loop is executed in the
    > normal way in the second part of the function when I enable the backup.
    > However, the file on disk remains empty. Next time I call the function,
    > the .bak file is "filled" again but with emptiness which in itself is
    > logical, there is nothing in the .lst file to be copied. However, when I
    > call the function with "0" again, the .lst file again is filled like it is
    > supposed to.
    >
    > I just can't figure out why the .lst file is not filled after the part of
    > the function that makes the backup has been executed. BTW I am not using
    > the files anywhere else in the program.
    >
    > Thank You very much in advance for any help!
    >
    > Yours sincerely,
    > Rene
    >
    > P.S. "Bestand" is Dutch for "file".
    >
    >
    > void TForm1::slaOp(bool backup)
    > {
    > fstream uitvoerBestand;
    > fstream backupBestand;
    > char letter;
    >
    > if (backup)
    > {
    > uitvoerBestand.open("Quizdata.lst", ios::in);
    > backupBestand.open("Quizdata.bak", ios::eek:ut);
    >
    > uitvoerBestand.get(letter);
    > while (!uitvoerBestand.eof())
    > {
    > backupBestand.put(letter);
    > uitvoerBestand.get(letter);
    > }
    > backupBestand.put(letter);
    >
    > backupBestand.close();
    > uitvoerBestand.close();
    > }
    >
    > uitvoerBestand.open("Quizdata.lst", ios::eek:ut);
    >
    > for (int teller = 0; teller < aantalDeelnemers; teller++)
    > {
    > uitvoerBestand << setfill('0') << setw(3) << setprecision(3);
    > uitvoerBestand << deelnemerLijst[teller].nummer;
    > uitvoerBestand << deelnemerLijst[teller].naam.c_str();
    > if (teller < aantalDeelnemers - 1)
    > {
    > uitvoerBestand << endl;
    > }
    > }
    > uitvoerBestand.close();
    > }


    uitvoerBestand was closed but not destroyed. After the first close
    operation, you probably end up with the failbit and eofbit set in your
    fstream object. After opening a file it is always a good idea to do some
    error checking.

    After
    fstream uitvoerBestand(Quizdata.lst", ios::eek:ut);
    do
    if(!uitvoerBestand.good())
    // error... do something

    your problemis solved by destroying uitvoerBestand before trying to reopen
    it

    like this...

    void TForm1::slaOp(bool backup)
    {
    char letter;

    if (backup)
    {
    fstream uitvoerBestand("Quizdata.lst", ios::in);
    fstream backupBestand("Quizdata.bak", ios::eek:ut);

    uitvoerBestand.get(letter);
    while (!uitvoerBestand.eof())
    {
    backupBestand.put(letter);
    uitvoerBestand.get(letter);
    }
    backupBestand.put(letter);

    backupBestand.close();
    uitvoerBestand.close();
    }

    fstream uitvoerBestand(Quizdata.lst", ios::eek:ut);

    for (int teller = 0; teller < aantalDeelnemers; teller++)
    {
    uitvoerBestand << setfill('0') << setw(3) << setprecision(3);
    uitvoerBestand << deelnemerLijst[teller].nummer;
    uitvoerBestand << deelnemerLijst[teller].naam.c_str();
    if (teller < aantalDeelnemers - 1)
    {
    uitvoerBestand << endl;
    }
    }
    uitvoerBestand.close();
    }


    ------------------------

    Eric Pruneau
    Eric Pruneau, Jul 30, 2008
    #2
    1. Advertising

  3. Rene

    Rene Guest

    "Eric Pruneau" <> wrote in message
    news:ty4kk.442$...
    > uitvoerBestand was closed but not destroyed. After the first close
    > operation, you probably end up with the failbit and eofbit set in your
    > fstream object. After opening a file it is always a good idea to do some
    > error checking.
    >
    > After
    > fstream uitvoerBestand(Quizdata.lst", ios::eek:ut);
    > do
    > if(!uitvoerBestand.good())
    > // error... do something


    I skipped this because the first function that is executed in the program
    fills the uitvoerbestand with parameter backup==0. So every time afterwards
    one is sure that the file exists. This is off course not taking into
    consideration that external stuff might happen, like someone deleting the
    file on purpose or a hd error.

    > your problemis solved by destroying uitvoerBestand before trying to reopen
    > it


    > fstream uitvoerBestand("Quizdata.lst", ios::eek:ut);


    I removed most of Your reply. This indeed works, You made me happy! I notice
    that the .open() function is not necessary in this case. I am quite new to
    C++ and especially to OOP so forgive me asking a question which is maybe
    very stupid, but am I correct that in de constructor of the class fstream
    with these parameters the .open() function is being called?

    Thank You very much, this helped me a lot!

    Yours sincerely,
    Rene
    Rene, Jul 31, 2008
    #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. John Kandell
    Replies:
    4
    Views:
    4,141
    eeebop
    Dec 10, 2004
  2. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,490
    Luis Esteban Valencia
    Jan 6, 2005
  3. Martin Hvidberg

    Q: Newbee. Read and convert CSV file...

    Martin Hvidberg, Feb 23, 2005, in forum: C Programming
    Replies:
    4
    Views:
    583
    Martin Hvidberg
    Feb 24, 2005
  4. Martin Arvidsson, Visual Systems AB

    NewBee Q: Saving TextBox data to Table and display a new page.

    Martin Arvidsson, Visual Systems AB, Mar 18, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    355
    David Wier
    Mar 18, 2008
  5. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Replies:
    1
    Views:
    194
    Curt_C [MVP]
    Oct 3, 2003
Loading...

Share This Page