I am writing a program to read a text file, do some calculations and output into another text file. The program runs fine but the problem I am having is that the numbers that are written to the text file aren't precise enough. They only go to 2 decimal points and I need the to go to at least 3. Here is the code where I convert the vector<long double> new_times
into a string so I can write it to the text file:
//Convert vector to string
vector<string> tempStr;
for (unsigned int i(0); i < new_times.size(); ++i){
ostringstream doubleStr;
doubleStr << new_times[i];
tempStr.push_back(doubleStr.str());
}
//Write new vector to a new times file
ofstream output_file("C:/Users/jmh/Desktop/example.txt");
ostream_iterator<string> output_iterator(output_file, "\n");
copy(tempStr.begin(), tempStr.end(), output_iterator);
I know that the vector has a higher precision than 2 decimal places because when I used the setprecision()
function in a cout
line the output was fine:
cout << setprecision(12) << new_times[3] << endl;
output: 7869.27189716
Can I use the setprecision()
function somehow when I am writing to the text file? Or Do I need to do something else? Any help would be appreciated.