why it can not exit the while loop

This is a discussion on why it can not exit the while loop within the C Programming forums, part of the General Programming Boards category; Here is an attachment named bunny.pdf. It is an OBJ file is read by this program. The problem now is ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    why it can not exit the while loop

    Here is an attachment named bunny.pdf. It is an OBJ file is read by this program.

    The problem now is it can not exit the while loop while it is running. And I do not why it read twice at each line of the OBJ file if I did not use fseek().

    Code:
    #include <stdio.h>
    typedef struct{
    	float p1;
    	float p2;
    	float p3;
    }pointv;
    
    typedef struct{
    	int  ia;
    	int  ib;
    	int  ic;
    }pointf;
    
    pointv vtemp;
    pointf ftemp;
    
    pointv va[510];
    pointf fa[1010];
    
    
    int main(void){
    	FILE *fp;
    	int  i=1;
    	int  j=0;
    	char mark;
    //	int  end;
    
    	fp=fopen(".\\bunny_1k.obj","rb+");
        
    	fscanf(fp,"%c %f %f %f", &mark, &vtemp.p1, &vtemp.p2, &vtemp.p3);
        va[i].p1=vtemp.p1;
    	va[i].p2=vtemp.p2;
    	va[i].p3=vtemp.p3;
    
    
    
    	while( mark!='f' ){
    		fprintf(stdout,"%c %f %f %f\n", mark, vtemp.p1, vtemp.p2, vtemp.p3);
    		i+=1;
    	fscanf(fp,"%c %f %f %f", &mark, &vtemp.p1, &vtemp.p2, &vtemp.p3);
    	    va[i].p1=vtemp.p1;
    	va[i].p2=vtemp.p2;
    	va[i].p3=vtemp.p3;
    	}
    
        fa[j].ia=(int)vtemp.p1;
        fa[j].ib=(int)vtemp.p2;
        fa[j].ic=(int)vtemp.p3;
    
    	while( !feof(fp) ){
    		j+=1;
    		fscanf(fp,"%c %d %d %d", &mark, &ftemp.ia, &ftemp.ib, &ftemp.ic);
    		fa[j].ia=ftemp.ia;
    		fa[j].ib=ftemp.ib;
    		fa[j].ic=ftemp.ic;
    		fprintf(stdout,"%c %d %d %d\n",mark, ftemp.ia, ftemp.ib, ftemp.ic);
    	}
    	return 0;
    }
    Attached Images Attached Images

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you really think you should use "rb+" when reading a text-file? The fact that it's called .obj doesn't change the fact that it is actually a text-file.

    Also, reading with %c is a bit dodgy in scanf() - you end up with a newline for %c, then it tries to read the next part as a %f or %d format, which ends up with a 'v' or 'f' - not a number, so it exits the fscanf with incomplete data. That's why you get two lines of fprintf() for each actual line in the input file, one with a 'v' or 'f' at the front, and another with "no mark". [At least that is what happens to me in my system].

    There are two solutions I can think of:
    1. Add \n to the end of the fscanf format string.
    2. Use fgets() to read a line, then use sscanf() to get the data out of the line.

    In both cases, validate that the *scanf() function you used actually got as many data items as it expects - in you case, it should return 4 because there are 4 %<something> in the format string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    Thumbs up

    Mats I am happying for you reply, thanks very much. That 's my first assignment. So now I got it I think.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    To me, it seems you are only doing one read and then going into a while loop. If that first character is NOT 'f', you aren't ever going to leave that while loop! Try putting your fscanf inside that while loop and you will probably solve that problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 03:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  3. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  5. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 09:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21