fprintf vs fout

This is a discussion on fprintf vs fout within the C++ Programming forums, part of the General Programming Boards category; The following thread describes a couple ways to skin the cat. http://cboard.cprogramming.com/showthread.php?t=21714 I started and got my program working using ...

  1. #1
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    Question fprintf vs fout

    The following thread describes a couple ways to skin the cat.
    Write some text to a file

    I started and got my program working using a simple logfile...
    Code:
        cout << "Log filename: ? ";
        cin >> logName;               // could put this in the main() and then echo to the logfile 
        ofstream fout(logName);   // open for writing                                               
        fout << logName << endl << endl;      //include date and time next
        while(1) { fout << lots of data ...}
    But now I want to align the columns of my output data, etc.

    How do I go about getting the fprintf formatting in std:: ofstream?
    I can't find the magic url with what I'm looking for.

    Thanks

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    have a look at the iomanip header. you probably want std::setw and std::setfill.

    I have to say that iostream formatting is pretty clumsy and verbose compared to printf (price you pay for type safety and extensibility, I guess)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    Answers found...Thanks, I also found these links with examples showing usage.

    http://www.cplusplus.com/reference/i.../manipulators/
    http://www.awprofessional.com/articl...70770&seqNum=4 (accelerated C++)

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I know C++ is significantly better than C in some areas. IMO, this isn't one of them.

    If I was really lazy or just wanted to get the project logic written out, I'd include <cstdio> and just use fprintf(), but that would require you to fopen() the file, so..... probably more work to switch them back and forth. Might as well just write it one way and leave it that way.

  5. #5
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    FYI - if you think of each line as a column, it helps in organizing code layout.

    Code:
            fout << dec << "Item: ";
            fout.width(3); fout << left << j << " Counts: ";
            fout.width(8); fout << data[j] << " Rate: ";
            fout.width(8); fout << rate[j] << endl;
    Dave.end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. fin fout, file pointer question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 08:49 AM
  4. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 11:55 AM
  5. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21