Like Tree6Likes
  • 1 Post By ZuK
  • 1 Post By ZuK
  • 2 Post By oogabooga
  • 2 Post By Salem

while(!feof(f))...

This is a discussion on while(!feof(f))... within the C Programming forums, part of the General Programming Boards category; Hi guys. I'm having a problem with the lecture of a FILE in Devc++. The file is: F 5 F ...

  1. #1
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    68

    while(!feof(f))...

    Hi guys.

    I'm having a problem with the lecture of a FILE in Devc++.

    The file is:

    F 5
    F 10
    F 17
    B 23 2
    F 42
    B 50 5
    C 70
    F 90
    The code I'm using to read the FILE is:

    Code:
    void assignaEspecial(char cMode, stCasella casella[97]){
         FILE *f;
         char strFitxer[50];
         stFitxer fitxer[97];
         int i = 0;
         int proba = 5;
         
         
         
         if(cMode == 'F' ||cMode == 'f'){
                  printf("Nom del fitxer: "); //File name
                  scanf("%s",strFitxer);
                  f = fopen(strFitxer,"r");
                  if( f == NULL){
                      printf("Error! Fitxer NULL\n");
                  }
                  else{ //Entra 
                      printf("Hello?!"); //This printf appears in the screen 
                      while(!feof(f)){ //Here crashes
                          
                          fscanf(f,"%c",fitxer[i].cTipus);
                          fscanf(f,"%d", fitxer[i].nCasella);
                          printf("%d\n",fitxer[i].nCasella);
                          if(fitxer[i].cTipus == 'B'){
                              fscanf(f,"%d",fitxer[i].nTorns);
                          }
                          i++;    
                      }
                    }
                  assignaFitxer(fitxer, casella, i);
                  fclose(f);
         }
         if(cMode == 'E' || cMode == 'e'){
              
              printf("HE ENTRADO EN E\n");
              for(i = 0; i < 96; i++){
                    if(i == 59 || i == 80){
                         casella[i].nEspecial = 2;
                         casella[i].nColor = 6;
                    }
                    if( i == 66 || i == 90){
                        casella[i].nEspecial = 3;
                        casella[i].nColor = 0;
                    }
                    if( i == 5 || i == 10 || i == 17 || i == 23 || i == 30 || i == 38 || i == 42 || i == 50 || i == 56 || i == 64 || i == 70 || i == 73 || i == 77 || i == 82 || i == 92){
                        casella[i].nEspecial = 1;
                        casella[i].nColor = 7;
                    }
              }
         }
         
                  
    }
    When executing, appears the printf: "Hello?!" but then, the program crashes.

    In another thread, anduril462 told me that using !feof(f) is not a good idea, but this is the only way to know when the file ends that our teachers showed us in the university, so I want to use it if it's possible.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        fscanf(f,"%c",fitxer[i].cTipus);
        fscanf(f,"%d", fitxer[i].nCasella);
    fscanf expects the address of the varibles to read.
    Kurt
    catasturslykid likes this.

  3. #3
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    68
    Quote Originally Posted by ZuK View Post
    Code:
        fscanf(f,"%c",fitxer[i].cTipus);
        fscanf(f,"%d", fitxer[i].nCasella);
    fscanf expects the address of the varibles to read.
    Kurt
    That was the error. Thank you so much!

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Generally there is nothing wrong to use feof(). You just have to know that EOF is set after scanning has failed, that means that you still have to check the return value of your fscanf() calls. If you do so then there is no more need to call feof()

    Kurt
    catasturslykid likes this.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Actually, there is something wrong with using feof to control the loop. It simply isn't designed for that. The problem is that you also want to stop reading if there is some other error condition that is not feof, as indicated by ferror.

    The reading/writing functions all return an indication that means either eof or error. The feof and ferror functions are provided for you to distinguish between these two possibilities conflated by the other functions.

    Your loop should be something like the code below. Note the space added to " %c" to skip spaces (and newlines). The %d's don't need the space before them since they skip spaces anyway.

    Also note that normal termination occurs when the first fscanf doesn't return 1. If any of the other fscanf's don't return 1, it's an error in the data and you could print an error message to that effect and perhaps terminate the program.
    Code:
        while(1){
            if (fscanf(f, " %c", &fitxer[i].cTipus) != 1) break;
            if (fscanf(f, "%d", &fitxer[i].nCasella) != 1) break;
            printf("%d\n",fitxer[i].nCasella);
            if(fitxer[i].cTipus == 'B'){
                if (fscanf(f, "%d", &fitxer[i].nTorns) != 1) break;
            }
            i++;    
        }
    vart and catasturslykid like this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > fscanf expects the address of the varibles to read.
    > Kurt
    > catasturslykid likes this.
    One week ago and three weeks ago, you were introduced to compiler options like -Wall which diagnose these very problems before you get to running the program.

    Pretty soon, we're all just going to get fed up of pointing out your repeated mistakes.
    Especially when those mistakes are easily detected and avoided, if you bothered to use the tools (and pay attention to the results) available to you.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while(!feof)
    By mconflict in forum C Programming
    Replies: 12
    Last Post: 04-02-2012, 12:30 PM
  2. HELP: feof
    By dlf723 in forum C Programming
    Replies: 5
    Last Post: 07-23-2010, 08:49 AM
  3. feof() from FAQ
    By salvadoravi in forum C Programming
    Replies: 6
    Last Post: 01-25-2008, 12:08 PM
  4. feof()
    By XSquared in forum C Programming
    Replies: 2
    Last Post: 06-02-2004, 12:16 AM
  5. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 11:38 AM

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