Professional Documents
Culture Documents
Week 15 Lab Practical
Week 15 Lab Practical
Question:
write a program in C to count the number of customers with educational qualification as phD who has ordered
food online .Display the count
to store the details of the customers (age, gender ,martial status,monthly income) who are self employeed into a
self file named customer_SelfEmployeed.csv
display the details of the customers(Age,Gender,Occupation) where monthly income is "No income"
Part1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
FILE *fp = fopen("onlinefood.csv", "r");
if (fp==NULL)
printf("No file");
char a[500];
int count = 0;
while (fgets(a, 500, fp) != NULL) {
char *age = strtok(a, ",");
char *gender = strtok(NULL, ",");
char *marital = strtok(NULL, ",");
char *occup = strtok(NULL, ",");
char *month_income = strtok(NULL, ",");
char *edq = strtok(NULL, ",");
if (strcmp(edq, "Ph.D") == 0)
count++;
}
printf("The number of people that have Ph.D is %d\n", count);
fclose(fp);
return 0;
}
Part2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
FILE *fp = fopen("onlinefood.csv", "r");
FILE *fw = fopen("customer_selfemployed.csv", "w");
if (fp == NULL || fw == NULL) {
printf("Error opening file.\n");
return 1;
}
char line[500];
fclose(fp);
fclose(fw);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
FILE *fp = fopen("onlinefood.csv", "r");
if (fp==NULL)
printf("No file");
char a[500];
printf("Age\tGender\tOccupation\n");
while (fgets(a, 500, fp) != NULL) {
char *age = strtok(a, ",");
char *gender = strtok(NULL, ",");
char *marital = strtok(NULL, ",");
char *occup = strtok(NULL, ",");
char *month_income = strtok(NULL, ",");
if (strcmp(month_income, "No Income") == 0)
printf("%s\t%s\t%s\n", age, gender, occup);
}
fclose(fp);
return 0;
}