-
Saving results to a file
Currently, I have a for-loop that goes on for a really long time, and each time, at the end, it does this:
cout << " " << endl;
cout << a << "x^2 + " << b << "x + " << c << endl;
cout << primes << " out of " << total << " were prime." << endl;
The problem is, the DOS window (in Win2000) ends up cutting off the top portion, because there are so many iterations. Is there a way to have the above information saved to a text file named "output.txt", so that each iteration in the loop gets saved into the file, but doesn't overwrite the one before it? I have no clue as to the correct syntax.
-
Well the simple answer, with no changes to your code, would be to run the program as follows
myprog > output.txt
-
Here is one solution.
-----
#include <fstream>
using std::ofstream;
using std::ios;
using std::endl;
int main()
{
ofstream output;
output.open("output.txt", ios::out);
for (int i = 0; i < 100; ++i)
output << "testing " << i << endl;
return 0;
}
-----
Try the algorithm above.
Kuphryn
-
Add this line right before return 0.
-----
output.close();
return 0;
-----
Kuphryn
-
i have a question
does it matter if we don't close the file ?
i wrote a program that opens a file and exits without fclosing it
and i got the results i wanted.
is it ok not to close the file ?
-
>is it ok not to close the file ?
On my system if the file isn't closed then nothing will be written to it and after the program terminates the file will be empty. The quick answer is to always free your memory and close your files just in case. That way you avoid potential surprises. :)
-Prelude
-
hrmmmmm
I tried what kuphryn said, and got these errors:
Compiling...
primesieve.cpp
primesieve.cpp(41) : error C2143: syntax error : missing ';' before ':'
primesieve.cpp(41) : error C2873: 'std' : symbol cannot be used in a using-declaration
primesieve.cpp(41) : error C2143: syntax error : missing ';' before ':'
primesieve.cpp(59) : error C2275: 'ios' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\iosfwd(254) : see declaration of 'ios'
primesieve.cpp(59) : error C2143: syntax error : missing ')' before ':'
primesieve.cpp(59) : error C2059: syntax error : ')'
Error executing cl.exe.
prime sieve.exe - 6 error(s), 0 warning(s)
Here are the first few lines of the program:
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include "string.h"
#include <fstream>
using std:ofstream;
using std::ios;
using std::endl;
using namespace std;
int main(){
short amin;
short amax;
short bmin;
short bmax;
long cmin;
long cmax;
short iterations;
float pmin;
float pmax;
ofstream output;
output.open("output.txt", ios:out);
The errors point to these lines:
using std:ofstream;
output.open("output.txt", ios:out);
Probably something stupid, oh well....
-
nevermind
NEVERMIND, it was that stupid smiley formatting, i missed two colons. Ok it works now :D
-
if you use the line
using namespace std;
then don't use the other lines. It is redundant and may cause problems.
ofstream is a dedicated output stream. Therefore you don't need to use the ios::out modifier. Use that modifier only if you are using an fstream as opposed to an ofstream.
If your code was outputting to the screen appropriately then just replace the cout << with output << or add a second line after each cout << with output << so you print to the screen as well as write to the file.
-
This thread is a perfect example of what the "Disable Smilies in This Post" button is for! :)
-
Oh yea, two other things...
It's not necessary to do
Code:
output.open("output.txt", ios::out);
As ios::out is redundant
And to clear up something for the others
Code:
#include <fstream>
using std:fstream;
using std::ios;
using std::endl;
int main()
{
ofstream output;
output.open("output.txt", ios:ut);
for (int i = 0; i < 100; ++i)
output << "testing " << i << endl;
output.close();
return 0;
}
In that code, output.close(); is also redundant because once the file object falls out of scope, the destructor is called and the file is automatically closed... you can however, do this... it's just not necessary here