Failure to reopen a file to update/append new data.



I sorted a file to find the file's 4 numbers that have the highest frequencies, wrote the 4 numbers on the screen, then wrote them in a file using:

[c++]
// Prints the 4 highest numbers on screen
cout << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
cout << indexoffirst+1 << " = " << first << ", " << indexofsecond+1 << " = " << second << ", " << indexofthird+1 << " = " << third << ", " << indexoffourth+1 << " = " << fourth << endl <<endl;
cout << endl << endl;

ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
/*
if (! d_file )
{
cout << "Can't open input file " << endl << endl;
cin.get();
exit(1);
}
*/

// Writes to file
d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
d_file << indexoffirst+1 << " = " << first << ", " << indexofsecond+1 << " = " << second << ", " << indexofthird+1 << " = " << third << ", " << indexoffourth+1 << " = " << fourth << endl <<endl;
d_file << endl << endl;
d_file.close(); // Close the file stream explicitly
[/c++]

I then re-sorted the same file to find the 4 numbers having the lowest frequencies. again, I wrote the 4 numbers on the screen, then I tried to reopen the same file to update/append the new data into the file using:

[c++]
// Prints the 4 lowest numbers on screen
cout << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
cout << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;

ofstream out1;
out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt", std::ios::app);
/*
if (! d_file )
{
cout << "Can't open input file " << endl << endl;
cin.get();
exit(1);
}
*/

// Writes to file
d_file << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
d_file << indexoffirst+1 << " = " << first << ", " << indexofsecond+1 << " = " << second << ", " << indexofthird+1 << " = " << third << ", " << indexoffourth+1 << " = " << fourth << endl <<endl;
d_file << endl << endl;
d_file.close(); // Close the file stream explicitly
[/c++]

However the file will not update/append the new data and I don't know why.


Replace [c++] and [/c++] with [code] and [/code]
You had the right idea ;)

As for your problem, you used std::ios::app but you forgot to use std::ios::out! It should look like this:
MyFile.open("MyFile.txt", std::ios::app|std::ios::out);
Why is this needed? Well, I suppose because it is to be consistent with std::fstream where it could be either in, out, or both.
Last edited on
I made the changes, but it still didn't update/append the new data to the file. In the build window I get the following:

1
2
3
4
5
6
7
8
'Mega_Money.exe': Loaded 'C:\Users\Therry.arkashea1-PC\Documents\Visual Studio 2010\Projects\Mega_Money\Debug\Mega_Money.exe', Symbols loaded.
'Mega_Money.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'Mega_Money.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'Mega_Money.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'Mega_Money.exe': Loaded 'C:\Program Files\AVAST Software\Avast\snxhk.dll', Cannot find or open the PDB file
'Mega_Money.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'Mega_Money.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The program '[3456] Mega_Money.exe: Native' has exited with code 0 (0x0).


What am I doing wrong? Thanks for your reply.
Why does this code seem to be the only solution to my problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Prints the 4 highest numbers on screen
cout << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
cout << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
cout << endl << endl;

// Prints the 4 lowest numbers on screen
cout << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
cout << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;	
		
ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );

// Writes to file
d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
d_file << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
d_file << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;	
		
d_file.close();   // Close the file stream explicitly 


And why didn't this code work? Why did the program refuse to write/append the new data to the file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );

/*
if (! d_file )
{
cout << "Can't open input file " << endl << endl;
cin.get();
exit(1);
}
*/

// Writes to file
d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
d_file << endl << endl;

d_file.close();   // Close the file stream explicitly


ofstream out1;
out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt", std::ios::app|std::ios::out);
		
/*
if (! d_file )
{
cout << "Can't open input file " << endl << endl;
cin.get();
exit(1);
}
*/

// Writes to file
d_file << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
d_file << endl << endl;


I now understand that the better way is to open the file just once, but I do not understand why the file was not updated/appended with the new data.

There's nothing wrong with opening and closing files as much as you want. You just have to write it to the right file...

on line 33...35 of your second part you write the data to the closed d_file and not out1
Wow! Thanks, how stupid of me. Problem solved.
Topic archived. No new replies allowed.