I am trying to write a simple program that reads in csv data from a text file, and then sums the values of the integers read in. I am new to C and trying to figure out small little programs to practice. I am able to get the file open, but it does not actually read in the values. Instead, it says the string read is simply the filename. I've been trying to figure it out. I know the actual code works because if I eliminate the file and input an array, it sums the values. Any ideas?

Here is the code:

#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: %s\n", ptr);
       
       sum = sum + x;
       ptr = strtok(NULL, ",");
       }
       fclose(file);
   }         
   printf ("The sum of the numbers is: %d\n",sum);
   getchar ();
}

Recommended Answers

All 4 Replies

all your program is doing is parsing the filename (see line 11). What you need to do is read a line from the file then call strtok() on the input buffer for that line, such as

char line[255];
while( fgets(line, sizeof(line), file)
{
    ptr = strtok(line, ","); // assume comma-deliminated csv file
    while(ptr != NULL)
    {
           // do something with this token
           //
           // now get next token
           ptr = strtok(NULL, ",");
    }
    fclose(file);
}

that made a HUGE difference...it works now!!!! I do have one question, though. Will this work even if we don't know how many values are in the file? I see that line has a size of 255...what if the file ends up having a lot more characters?

>>Will this work even if we don't know how many values are in the file?
Yes

>> see that line has a size of 255...what if the file ends up having a lot more characters?
That is the number of characters in just one line, not the entire file. The contents of the previous line is tossed into the bit bucket when a new line is read via the while statement. If any one line is longer than 255 characters (unlikely) then increase 255 to fit the line size. I just picked the value 255 out of the air because its unlikely csv files will have longer lines. Allocating too much input buffer space is not normally a problem, but allocating too little can be disasterous.

Some melancholy remarks:

The most serious defect of strtok-based CSV parsing is that strtok skips null values. For example, in the line "1st,,3rd," strtok selects only two tokens: 1st and 3rd.

In practice most of CSV exporters (DBMS especially) write this line for four data fields tuple (record) with null 2nd and 4th values, not for two-fields. Obviously, strtok-based parsing keeps silent with wrong result for this line.

Besides that a strtok-based CSV parser can't process quoted text CSV data fields.

Regrettably, an adequate CSV parser is not a 3-5 source lines function :(
Fortunately, it's not so hard to write a good CSV parser ;)

About common CSV specifications see:
http://en.wikipedia.org/wiki/Comma-separated_values

commented: good points :) +36
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.