Reading space separated values file in c++ error -
i trying float values out of file use them in program. used following forum construct program read file line line.
values obtained doing appear truncated @ end.
my code
#include <iostream> #include <cstring> #include <sstream> #include <fstream> using namespace std; int main() { ifstream file; file.open("test_file.ssv"); string line; while(file.good() && (getline(file, line))) { istringstream iss(line); double a, b, c; iss >> >> b >>c ; cout << <<endl; } file.close(); return (0); }
the output obtain
9292.31 32432.2
while file has following data
9292.3123 4234.66 342.25423 32432.2423 3423.656 341.67841
any suggestions improve upon this?
your standard stream may have low floating point precision , therefore see few of decimals when outputing float
std::cout
. use std::ios_base::precision
increase precision , using std:: ios::floatfield
output fixed or scientific precision , example:
// modify precision #include <iostream> // std::cout, std::ios int main () { double f = 3.14159; std::cout.unsetf ( std::ios::floatfield ); // floatfield not set std::cout.precision(5); std::cout << f << '\n'; std::cout.precision(10); std::cout << f << '\n'; std::cout.setf( std::ios::fixed, std:: ios::floatfield ); // floatfield set fixed std::cout << f << '\n'; return 0; }
outputs:
3.1416 3.14159 3.1415900000
Comments
Post a Comment