[Newbee] Problem with saving a file

R

Rene

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();
}
 
E

Eric Pruneau

Rene said:
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();
}
 
R

Rene

Eric Pruneau said:
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
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top