Write a C program which reads and access the elements of the csv file by using the system call functions such as read, write and open. You should sum all the no. present inside a particular column in that csv file . by using the system call functions.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum=0;
FILE *myFile;
myFile = fopen("number.csv", "r");
int numberArray[10];
int i;
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < 10; i++)
{
fscanf(myFile, "%d,", &numberArray[i] );
}
for (i = 0; i < 10; i++)
{
sum=sum+(numberArray[i]);
}
fclose(myFile);
printf("Sum is: %d\n\n", sum);
return 0;
}
Comments
Leave a comment