Output format to file

This is a discussion on Output format to file within the C++ Programming forums, part of the General Programming Boards category; 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 ...

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

    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,675
    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
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,091
    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
    137
    If not cout, then what should I be setting the options for?

    Best,
    Niles.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,091
    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
    137
    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, 10: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, 12: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, 05:54 AM

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