Thread: Saving results to a file

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    5

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the simple answer, with no changes to your code, would be to run the program as follows

    myprog > output.txt

  3. #3
    kuphryn
    Guest
    Here is one solution.

    -----
    #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;

    return 0;
    }
    -----

    Try the algorithm above.

    Kuphryn

  4. #4
    kuphryn
    Guest
    Add this line right before return 0.

    -----
    output.close();
    return 0;
    -----

    Kuphryn

  5. #5
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    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 ?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    5

    Unhappy 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 stdfstream;
    output.open("output.txt", iosut);

    Probably something stupid, oh well....

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    5

    nevermind

    NEVERMIND, it was that stupid smiley formatting, i missed two colons. Ok it works now

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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:ut 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.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    This thread is a perfect example of what the "Disable Smilies in This Post" button is for! :)

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM