I am an extreme newbie to C and am trying to write a "simple" program that opens a csv text file of integers, parses them, and then sums them. I wrote what I thought should work, but it does not actually retrieve the integer values to sum. The only token it reads is the filename. Could someone help????
Here is what I have.
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int x; int sum; char filename[] = "data.txt"; FILE *file = fopen(filename,"r"); char *ptr; ptr = strtok(filename, ","); sum = 0; if (file == NULL) { printf("File could not be opened.\n"); getchar (); } else { while (ptr != NULL) { x = atoi(ptr); printf("The string is: %d\n", ptr); sum = sum + x; ptr = strtok(NULL, ","); } fclose(file); } printf ("The sum of the numbers is: %d\n",sum); getchar (); }