Thread: Output format to file

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Output format to file

    Hi

    I am writing data from two vectors to a file in the following way:

    Code:
    std::ofstream data_file("data.txt", ios::out);
    j = Y.begin();
    for(i=X.begin(); i<X.end(); i++)
    {
    vectorFile << *i << "\t" << *j << "\n";
    j++;
    }
    data_file.close();
    However, is there a way to format the data output? E.g., such t hat there is 8 numbers in total, i.e. of the form

    123.45678
    12.345678
    1.2345678
    ....

    I have tried with
    Code:
    	std::cout.setf ( std::ios_base::right, std::ios_base::basefield );
    	std::cout.width ( 5 );
    but that didn't do any difference.

    Best,
    Niles.
    Last edited by Niels_M; 04-02-2012 at 01:02 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    #include <iomanip>
    
    ...
    
    double d = 123.45678;
    
    ...
    
    std::cout << d << std::endl;
    std::cout << std::setprecision(8) << d << std::endl;
    Output:
    Code:
    123.457
    123.45678
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You do know that setting formatting options for cout will not affect your file stream (vectorFile) correct? Also note that setw() is not "sticky", meaning that it only works once you need to reissue the setw() each time you need it. Also when setting the flags don't forget the ios::showpoint and ios::fixed. See this link for manipulators.

    Jim

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    If not cout, then what should I be setting the options for?

    Best,
    Niles.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    If you want to alter how your file looks then modify the file stream. If you want to alter how cout displays the information then use cout. These are separate streams, changes to one does not change the other.

    Jim

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    I see, thanks!

    Best,
    Niles.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. save output file to excel format
    By imjustnew in forum C++ Programming
    Replies: 18
    Last Post: 12-31-2011, 11:09 AM
  2. How to format print for Hex like output
    By Teropita in forum C Programming
    Replies: 2
    Last Post: 05-07-2011, 06:59 PM
  3. output format
    By Beowolf in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2007, 01:22 AM
  4. output format: screen vs .txt
    By -JM in forum C++ Programming
    Replies: 7
    Last Post: 04-20-2006, 08:05 PM
  5. format numbers output?
    By DanC in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2006, 06:54 AM