Thread: File IO Help Please

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

    Angry File IO Help Please

    I want to read from the text file :
    Code:
    John,1,2,3,4
    Smith,2,3,4,5
    Jones,4,5,6,7
    How would I detect an empty line in the file and tell the user that the line is missing? For example (the line with 'Smith' is empty probably due to a typo and I want to detect it and tell the user that the line is missing. Below is my code so far, though it doesn't seem to work. Can somebody help me please?

    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("\nYou have an empty line"); /*This tells user that there is an empty line*/
    
    }
    
    fclose(readfile);
    return (0);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>How would I detect an empty line in the file
    Depending on what you mean by empty, it maybe as simple as checking for a string length of 0. But what if the line constists of a few spaces, would you consider that empty too? If so, string length wouldn't help, you'd need to interegate the string itself.

    >>and tell the user that the line is missing?
    Err.. printf() ?

    >>For example (the line with 'Smith' is empty probably due to a typo and I want to detect it and tell the user that the line is missing.
    I'm not sure I understand, if the line is empty how do you know it was supposed to contain Smith's name and details?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User mlsbbe's Avatar
    Join Date
    Mar 2003
    Posts
    24
    argg..sorry. Its probably because I only posted part of my code here.
    I've just had this idea and I'll try work on it. Thankyou for the help .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 10:18 AM