FscanF Program falls over

This is a discussion on FscanF Program falls over within the C Programming forums, part of the General Programming Boards category; Code: void read() { FILE *walls; if((walls=fopen("walls", "r")) == NULL) { printf("Cannot open file\n"); } while (fscanf(walls, "%f" "%f" "%f" ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    24

    FscanF Program falls over

    Code:
    void read()
    {
    FILE *walls;
    
      if((walls=fopen("walls", "r")) == NULL) 
      {
        printf("Cannot open file\n");
      }
    		while (fscanf(walls, "%f" "%f" "%f" "%f" "%f" "%f" "%f" "\n",&t,&x,&y,&z,&w,&l,&h)!= EOF)
    		{
    		
    		t1 = t;
    		drawWall(t1,x,y,z,w,l,h);
    		//fprintf(stdout,"%d\n %f\n %f\n %f\n %f\n %f\n %f\n",t1,x,y,z,w,l,h); 
    	   }
    }
    basically visual studio keeps falling over I think it get to the end of the file and tries to keep going and i am not sure how to stop it doing so any help would be welcomed

    visual studio reports stream !=NULL

    Regards,

    gda2004

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    while (fscanf(walls, "%f" "%f" "%f" "%f" "%f" "%f" "%f" "\n",&t,&x,&y,&z,&w,&l,&h)!= EOF)
    Your fscanf is wrongly written.
    Code:
    fscanf(fp,"%d%d%d",&i,&j,&k); //look at all the %d, how the double quotes are used
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should probably check to see that fscanf returns seven. And why are you splitting those into tiny strings instead of just one?
    Code:
    while( fscanf( fp, "%f %f %f %f %f %f %f\n", ... ) == 7 )
        ...
    Or, you could just use fgets, and combine it with sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BEN10 View Post
    Your fscanf is wrongly written.
    Code:
    fscanf(fp,"%d%d%d",&i,&j,&k); //look at all the %d, how the double quotes are used
    Consecutive string literals will be combine into a single one.
    Code:
    printf( "Hello" " " "World" "!" "\n" );
    Doesn't make it any less ugly, but it's technically fine.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    24

    it

    Code:
    while (fscanf(walls, "%f %f %f %f %f %f %f \n",&t,&x,&y,&z,&w,&l,&h)!= EOF)
    I did changed it and it still did not work as it should same error

    I even removed the \n to make sure it was not that

    Regards,

    gda2004

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by quzah View Post
    Consecutive string literals will be combine into a single one.
    Code:
    printf( "Hello" " " "World" "!" "\n" );
    Doesn't make it any less ugly, but it's technically fine.


    Quzah.
    Thanks for the info quzah.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    i checked it is returning seven values each time

    Regards,

    gda2004

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well if it's returning seven each time, then it's not fscanf that's crashing your program.
    Code:
    #include<stdio.h>
    int main( void )
    {
        float foo[7];
        FILE *fp;
    
        fp = fopen( "walls", "r" );
        if( fp )
        {
            while( fscanf( fp, "%f %f %f %f %f %f %f\n", &foo[0], &foo[1], &foo[2], &foo[3], &foo[4], &foo[5], &foo[6] ) == 7 )
                printf( "%f %f %f %f %f %f %f\n", foo[0], foo[1], foo[2], foo[3], foo[4], foo[5], foo[6] );
            fclose( fp );
        }
    
        return 0;
    }
    Does that work? Assuming I didn't typo anything, that should be fine. If it doesn't, what does it do, and what do you expect it to do instead?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    i think what my problem was I did not close the file at the end oops lol thanks for your help the problem appears to have gone away

    Regards

    gda2004

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by gda2004 View Post
    i think what my problem was I did not close the file at the end oops lol thanks for your help the problem appears to have gone away

    Regards

    gda2004
    Me too didn't notice it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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