fgets error handling

This is a discussion on fgets error handling within the C Programming forums, part of the General Programming Boards category; Does anybody know why my code for error handling in fgets doesn't work? I've been trying to make this work ...

  1. #1
    Registered User mlsbbe's Avatar
    Join Date
    Mar 2003
    Posts
    24

    fgets error handling

    Does anybody know why my code for error handling in fgets doesn't work? I've been trying to make this work for ages, and still cannot get this to work. What happens is that instead of skipping the line:

    Code:
    printf("\nInput line too long for buffer\n")
    when p_str!=NULL, it prints the string out as well.

    Thanks in advance.
    Below part of my code (though not all of it):

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXSTUDENT 40
    #define MAXNAME 20
    
    /*Declaring Function Prototypes*/
    int extract (char *copy_str, char *name_str, int length, student_t *s,int *p_index); 
    
    int main(void)
    {                 
    FILE *readfile;
    char str_copy[40];
    char str_name[MAXNAME+1];
    char *p_str;
    int i;                           
    
    student_t s [MAXSTUDENT]; 
    int num=0; 
    int result,index;
    char *p_error;
    
                                  
    if((readfile=fopen("datafile.txt","rt"))==NULL)
    {
    printf("Error, unable to read file.  Program exiting");
    return (1);
    }else
     {
     
     for(index=0;((p_error=fgets(str_copy,sizeof(str_copy),readfile))!=NULL);index++)
            
                    {
                            if((p_str=strchr(str_copy,'\n'))!=NULL)
                                    {
                                    *p_str='\0'; /*fgets may not read \n, because data might me overwritten*/
                                    result=extract(str_copy,str_name,MAXNAME,s,&index);
                            
                                    } else
            
                                    {
                                    printf("\nInput line too long for buffer\n"); //This line is printed out when p_str!=NULL
    
                                    exit(0);
                                    }
                    }
     
    
     if(p_error==NULL){
      printf("Error in reading file"); /*EOF error*/
      }
    
    }        
    
    
    fclose(readfile);
    
    return (0);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I don't see a problem. When the buffer is filled and no newline is read, the error message is printed and the program terminates. Otherwise extract is called.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User mlsbbe's Avatar
    Join Date
    Mar 2003
    Posts
    24

    Angry Stuck on this for ages....Please Help!

    I've remade the file so that now I can compile it. However, this time, it doesn't skip out the line
    Code:
    if (p_error==NULL)
      {  
      printf("\nError in reading file"); /*EOF error*/
      }
    even when the following line condition is true:
    Code:
    while((p_error=fgets(str_copy,sizeof(str_copy),readfile))!=NULL)
    Below is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXSTUDENT 40
    #define MAXNAME 20
     
    typedef struct{
    char name[MAXNAME+1];
    int ex1,ex2,ex3,ex4,average;
    }student_t;
    
    
    int main(void)
    {                 
    FILE *readfile;
    char str_copy[40];
    char str_name[MAXNAME+1];
    char *p_str;
    int i,index;                           
    
    student_t s [MAXSTUDENT]; 
    
    
    char *p_error;
    
                                  
    if((readfile=fopen("results.txt","rt"))==NULL)
    {
    printf("Error, unable to read file.  Program exiting");
    return (1);
    }else
     {
     
      
                    while((p_error=fgets(str_copy,sizeof(str_copy),readfile))!=NULL)
            
                    {
                    index=0;
                    if((p_str=strchr(str_copy,'\n'))!=NULL)
                       
                       {
                       *p_str='\0';
                       printf("\nInput line OK");
                       } else
                            
                            {
                            printf("\nInput line too long for buffer\n"); 
                            exit(0);
                            }
                            
                    index++;
                    
                    }
                    
      if (p_error==NULL)
      {  
      printf("\nError in reading file"); /*EOF error*/
      }
    
    }
    
    fclose(readfile);
    return (0);
    }
    Please Can somebody help me? I dunno why this doens't work.
    Last edited by mlsbbe; 03-24-2003 at 12:51 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I can't see a need for this bit:
    Code:
        if (p_error == NULL)
        {
          printf("\nError in reading file");  /*EOF error*/
        }
    Also, I can't see or understand your problem. Can you try explaining it in more detail please...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User mlsbbe's Avatar
    Join Date
    Mar 2003
    Posts
    24
    i've just started a new thread -
    'File IO help please'
    and i've explained my problem better I think. Thankyou for the help! T

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i've just started a new thread
    Err. ok, I'll close this one. But please don't create unnecessary threads, we could have continued here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

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