In: Computer Science
Write a program that reads in a continuous stream of strings representing a line of CSV data in the format "NAME,AGE,EMAIL,DOB". Implement a function check_csv which verifies that each input string matches this format by ensuring that: • There are 4 tokens in the string corresponding to the 4 properties above. • The second token MUST be a number. • The fourth token should be in the format MM-DD-YYYY (hint: tokenize this as well). The function should return 0 if the CSV string is valid, and 1 otherwise. Your program should read CSV strings until the string "END" is entered as input. Print all valid strings to stdout followed by a line indicating how many invalid strings were entered. • You can use a character array with a buffer size of 1024 for reading each line using fgets. • You may not use any library specifically for parsing CSV. • Save your code as prob5.c.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int isleap(int year) //return 1 if leap year
{
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
int isvaliddate(int d,int m,int y) //return 1 if date is valid
{
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isleap(y))
month[1]=29;
if(y>=1900 && y<=9999)//check year
{
if(m>=1 && m<=12) //check month
{
if(d>=1 && d<= month[m-1])
return 1;
else
return 0;
}
else
return 0;
}
else
return 0;
}
int check_csv(char *data)
{
int isvalid=0; // let string is valid
char dummy[100]; // dummy string to copy data
strcpy(dummy,data); // strcpy to copy a string to other
char* token = strtok(dummy,","); // tokenizing string
char *attr[4]; // to store 4 tokens
int i=0;
while (token != NULL)
{
attr[i++]=token; //assign token in array
token = strtok(NULL,","); //further token the string
}
//checking if attr[1] is number or not
int count=0,flag=0;
for(int i=0;i<strlen(attr[1]);i++)
{
if(isdigit(attr[1][i]))
count++;
if(attr[1][i]=='.')//for a float
{
flag=1;
}
}
if(flag==0 && count==strlen(attr[1])) //valid case
;
else if(flag==1 && count==strlen(attr[1])-1)
;
else
isvalid=1;
//check date is valid or not
char dummy2[100]; //to copy date value
strcpy(dummy2,attr[3]); // strcpy to copy a string to other
char* date_token = strtok(dummy2,"-"); // tokenizing string
char *date[3]; // to store 3 tokens
i=0;
while (date_token != NULL)
{
date[i++]=date_token; //assign token in array
date_token = strtok(NULL,"-"); //further token the string
}
if(strlen(date[0])!=2)
return 1;
if(strlen(date[1])!=2)
return 1;
if(strlen(date[2])!=5)//since space is also counted in it
return 1;
// converting string to int
int dd,mm,yyyy;
sscanf(date[0],"%d",&mm);
sscanf(date[1],"%d",&dd);
sscanf(date[2],"%d",&yyyy);
//check if date is valid or not by calendar
if(!isvaliddate(dd,mm,yyyy))
return 1;
return isvalid;
}
int main()
{
char *fname="data.csv"; //change file name
//printf("Enter file name: ");
//scanf("%[^\n]%*c", fname);
FILE *file;
file = fopen(fname, "r");//open file in read mode
int i = 0;
char line[1024]; //store line
int inv=0; //count invalid lines
while (fgets(line, 1024, file))
{
if(strcmp(line,"END")==0)//if End appear break input stream
break;
else
{
if(!check_csv(line))// if valid print
printf("%s",line);
else
inv++;
}
}
printf("\nTotal no. of invalid lines are: %d\n",inv);
return 0;
}
Input: CSV file
Output: