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);
}