Thread: Read line

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post Read line

    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.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Ron
    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.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    The strtok function may do the trick.
    http://www.cppreference.com/stdstring/strtok.html
    silk.odyssey

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    10
    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];

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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);
    		
    	}
    You make k = 0 on every iteration. Unintended most likely.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  3. easy Q -> read a whole line from file
    By paperbox005 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2004, 03:58 PM
  4. Read each line of text file.
    By Mithoric in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 11:53 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM