-
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.
-
Code:
#include <iostream>
#include <iomanip>
...
double d = 123.45678;
...
std::cout << d << std::endl;
std::cout << std::setprecision(8) << d << std::endl;
Output:
-
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
-
If not cout, then what should I be setting the options for?
Best,
Niles.
-
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
-
I see, thanks!
Best,
Niles.