Is it possible to copy one word at a time from 1 line in the input file; or
it is only possible to character using getchar().
Please explain.
Is it possible to copy one word at a time from 1 line in the input file; or
it is only possible to character using getchar().
Please explain.
You could parse the returned string and seperate the words or, you could use fgetc to return one byte at a time and do a constant check for ' ' (space) to denote the end and begining of a word.Originally Posted by Ron
The strtok function may do the trick.
http://www.cppreference.com/stdstring/strtok.html
silk.odyssey
Here you go - messy but I only just got it to work. No good reason to use the vector XYZ. It gets the xyz coords of a point from an ascii file and (will) whack them in an image.
Just I have 300MB files, so it works for a while then I get an access violation at x0000000 ish. Why?
Code:int k = 0; char line[50]; double XYZ[3]; char *token; char seps[] = " ,\t"; while (! ascRast.eof() ) { ascRast.getline(line, 50); long ij[2] = {0, 0}; double XYZ[3]; token = strtok(line, seps); XYZ[0] = atof(token); while (token != NULL) { k = 0; k++; token = strtok(NULL, seps); if (k == 1) XYZ[1]= atof(token); if (k==2) XYZ[2] = atof(token); } i = XYZ[0] - xMin; j = yMax - XYZ[1]; vOut.fltVal = XYZ[2];
Here's an idea! Pick one: C or C++. Not both. Pick one. Then try your code again.
Quzah.
Hope is the first step on the road to disappointment.
You make k = 0 on every iteration. Unintended most likely.Code:while (token != NULL) { k = 0; k++; token = strtok(NULL, seps); if (k == 1) XYZ[1]= atof(token); if (k==2) XYZ[2] = atof(token); }
> while (! ascRast.eof() )
Check the FAQ on why using eof() in a control loop is bad.
> then I get an access violation at x0000000 ish. Why?
My guess is your first strtok() call returned NULL, because the string was empty.
So when you did this
XYZ[0] = atof(token);
it barfs.
Why would it be empty?
Well ascRast.getline(line, 50); isn't checked for errors, and since you've no idea what's in this line when it reaches EOF (because of your broken while loop), then all sorts of problems emerge.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.