I am attempting to read in a comma delimited file and only take in the first three values.

At the moment the code I have obviously does not work.

The text file is laid out like this:

1249968.646,16761001.880,1206.990,1,2,104,3,218210.549527

At the moment this is what my read in code looks like:

if(canopyfile)
{
	string token;
	stringstream iss;
	while ( getline(canopyfile, line) )
	{
		iss << line;
		while ( getline(iss, token, readinmode) )
		{
			x = token;

			getline(iss, token, readinmode);
			y = token;

			getline(iss, token, readinmode);
			z = token;

			iss.clear();

		}


		holder.setx(atof(x.c_str()));
		holder.sety(atof(y.c_str()));
		holder.setz(atof(z.c_str()));

		canopyvec.push_back(holder);
	}
} else {
	cout << "Can not Open the Canopy File!" << endl;
	usage();
}

This wouldn't be much of a problem if I knew all my files would be formatted the same way, but some switch where the values are and even the number of values, except for the first three.

Any help would be appreciated, Thanks in advance.

Cameron

I am attempting to read in a comma delimited file and only take in the first three values.

At the moment the code I have obviously does not work.

The text file is laid out like this:

1249968.646,16761001.880,1206.990,1,2,104,3,218210.549527

At the moment this is what my read in code looks like:

if(canopyfile)
{
	string token;
	stringstream iss;
	while ( getline(canopyfile, line) )
	{
		iss << line;
		while ( getline(iss, token, readinmode) )
		{
			x = token;

			getline(iss, token, readinmode);
			y = token;

			getline(iss, token, readinmode);
			z = token;

			iss.clear();

		}


		holder.setx(atof(x.c_str()));
		holder.sety(atof(y.c_str()));
		holder.setz(atof(z.c_str()));

		canopyvec.push_back(holder);
	}
} else {
	cout << "Can not Open the Canopy File!" << endl;
	usage();
}

This wouldn't be much of a problem if I knew all my files would be formatted the same way, but some switch where the values are and even the number of values, except for the first three.

Any help would be appreciated, Thanks in advance.

Cameron

How about something like this:

ifstreeam canopyfile;
// code to open canopyfile
string value1, value2, value3;
getline (canopyfile, value1, ',');
getline (canopyfile, value2, ',');
getline (canopyfile, value3, ',');
canopyfile.close ();

http://www.cplusplus.com/reference/string/getline.html

Does that solve it or is it more complicated and I am missing something? Seems like you should just read in your three values, then close. If you know your first three values end with commas and you don't care about anything after them, that should do it, I think.

Oops forgot to explain that there is about 100,000 lines just like that last one. So I need to loop through each line, pull the first three values, then go to the next line.

I hope that answers your question a bit, so no just reading and closing the whole file won't work.

Cameron

The following might work for you, assuming that the values are always comma-separated

string line;
	double x,y,z;
	char dummy; // for skipping commas

	while ( getline(canopyfile, line) )
	{
		stringstream iss(line);

		if(iss >> x >> dummy >> y >> dummy >> z)
		{
			holder.setx(x);
			holder.sety(y);
			holder.setz(z);

			canopyvec.push_back(holder);
		}
		else
		{
			// failed to extract 3 values
		}
	}
commented: Thank you again +1

The following might work for you, assuming that the values are always comma-separated

string token;
	double x,y,z;
	char dummy; // for skipping commas

	while ( getline(canopyfile, line) )
	{
		stringstream iss(line);

		if(iss >> x >> dummy >> y >> dummy >> z)
		{
			holder.setx(x);
			holder.sety(y);
			holder.setz(z);

			canopyvec.push_back(holder);
		}
		else
		{
			// failed to extract 3 values
		}
	}

Maybe I am just being dumb in this situation, but wouldn't there then need to be spaces like so:

109884 , 109278 , 1078508

That might just be me reading something wrong though.

Cameron

Maybe I am just being dumb in this situation, but wouldn't there then need to be spaces like so:

109884 , 109278 , 1078508

No need for spaces, although there can be tabs/spaces, but those will be ignored. It will work as long as there is a separating comma (or any other character that cannot be taken as part of value, for that matter).

This actually simplified my code quite a bit in the long run. Thank you very much. Also learned something new from the whole situation. So thanks again.

Cameron

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.