Hi everyone,
I have some troubles with reading floats from a file into a 2d vector. Basically, I have 3 coordinates (x,y,z) of some points which I want to store in a 2d vector. This is what I have so far:

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]){

	if (argc != 2){
		cout << "need a file..." << endl;
		return 1;
	}

	int i=1;
	float x,y,z;
	string line;
	vector< vector<float> > basis;
	vector<float> row;
	ifstream fin(argv[1]);
	istringstream inputString(line);

	while(i<4 && getline(fin, line) ){
	        inputString >> x >> y >> z;
		row.push_back(x);
		row.push_back(y);
		row.push_back(z);
		basis.push_back(row);
		row.clear();
		i++;
	}

	for(int l=0; l<3; l++){
	    for(int m=0; m<3; m++){
	      cout << basis[l][m] << " ";
	    }
	    cout << endl;
	}
	return 0;
}

The input file has the following format:
e.g.

1.33939344  3.498494944  1.33891
8.8484844    12.39398        4.59582111
23.9333         7.7893933      5.2323323
etc.

and the output that I get printed on the screen:

0 0 5.89483e-39 
0 0 5.89483e-39 
0 0 5.89483e-39

Can anyone help me with that?

Recommended Answers

All 6 Replies

I solved my problem, but I'm not sure if I understood that completely. Anyway, I had to place the line istringstream inputString(line) into the while loop. Then it works.

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]){

	if (argc != 2){
		cout << "need a file..." << endl;
		return 1;
	}

	int i=1;
	float x,y,z;
	string line;
	vector< vector<float> > basis;
	vector<float> row;
	ifstream fin(argv[1]);

	while(i<4 && getline(fin, line) ){
	        istringstream inputString(line);
	        inputString >> x >> y >> z;
		row.push_back(x);
		row.push_back(y);
		row.push_back(z);
		basis.push_back(row);
		row.clear();
		i++;
	}

	for(int l=0; l<3; l++){
	    for(int m=0; m<3; m++){
	      cout << basis[l][m] << " ";
	    }
	    cout << endl;
	}
	return 0;
}

There is also another small problem. It truncates the numbers from the file. How can I control that in istringstream?

How many digits are in each element of basis and how many do you see when printing them to screen in line 35?

I would drop the istringstream it's complicating things when it's not really necessary. I changed a couple of lines in your code, specifically the while loop condition

while(fin >> x >> y >> z) {
	   	row.push_back(x);
		row.push_back(y); 
		row.push_back(z);
  
                 //... rest is the same
          }

And added: cout <<fixed<<setprecision(10); before your output (around line 32).
You were getting the digits it's just that cout didn't know how many to display. (add <iomanip> to your includes for those methods).

That'll teach me to skip over the first post and go right the second. sheeeesh!

The reason why I used istringstream is because some of my lines in the file have more than 3 column since there is some additional information about the points. And I want to read this separately into a string vector.
For instance, the file can look like this:

1.23  33.222444    4.13243434    blah blah blah
3.009333  8.33243    1.0  blah
0.3343431  0.7334340 0.000000

I didn't know how to do that with istream.
I already figured out that I can specify the precision of my floats before it's printing out. But sometimes it prints something else than was given in the file. For instance, the file contains 1.46, and assume I want to print the last 8 digits. Then I get 1.45999908.
Or instead of 12.4873721, I get 12.48737240.
I used the following:.

cout.setf(ios::fixed);
cout.precision(8);
cout.setf(ios::showpoint);

Where's my mistake?

Unfortunately you've stumbled upon a problem inherent in floating point computations. See http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding . Try using a double -- there's really no reason to use float these days unless you're trying to cram numbers in as tightly as you can and if your precision requirements aren't very strict.

(See the wiki articles on "double precision floating-point" and "single precision floating-point" or search for "IEEE 754" if you're interested)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.