By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

[Newbee] Problem with saving a file

P: n/a
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::out);

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

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

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

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();
}
Jul 30 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Rene" <my*****@is.nobodya écrit dans le message de news:
R_*************@newsfe24.ams2...
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::out);

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

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

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

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

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

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

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

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
Jul 30 '08 #2

P: n/a
"Eric Pruneau" <er**********@cgocable.cawrote in message
news:ty**************@read2.cgocable.net...
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::out);
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::out);
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
Jul 31 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.