How to read a CSV file in c

Written by matt billock
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to read a CSV file in c
Tabulation and calculation programs can output data in CSV format (calculation image by lefty from

Many software products that deal with numbers and calculations have the ability to output data into a Comma Separated Value (CSV) file. This format can be an effective way of transporting data between different programs, as it is readable and fairly easy to manipulate. Many C programs that deal with data will likely have to read from a CSV file at some point.

Skill level:

Things you need

  • Text editor
  • Compiler
  • CSV file input

Show MoreHide


  1. 1

    Consult the documentation of the program that is providing the CSV file. Determine the number of fields in each line, as well as the format of each field. For example, if a program provides a CSV with the following data:

    1, "test", 34.5

    You would mark down three fields: one integer, one string and one floating point number.

  2. 2

    Create a structure containing a data member for each field identified in the CSV. Using the provided example line of 1, "test", 34.5 you would need the following structure:

    struct data


    int col 1;
    char *col 2;
    float col 3;


  3. 3

    Create a method in your program that will handle reading the CSV file. This will need to be accessible to the rest of your program, and will likely need to work on common data structures so that other methods can access the data that has been read in. Pass the parameter by reference to remove the need for a return value. An example function prototype would be:

    void ParseCSV( char *filename, data& input );

  4. 4

    Include the standard IO header using the following code:

    include <stdio.h>

    Add this code to the top of the source file that will be reading the CSV.

  5. 5

    Include the string library to enable manipulation of the CSV data using the following code:

    include <string.h>

    Add this code to the top of the source file that will be reading the CSV.

  6. 6

    Create a file object, which will read in the data, using the following code:

    FILE * pInput;

  7. 7

    Create a character buffer large enough to hold one line of the file at a time. Due to constraints of the language, the most straightforward way to do this is to declare a character array of a sufficiently large size, as with:

    define BUFFER_SIZE 1024

    char buf[BUFFER_SIZE];

  8. 8

    Open the file with the following code, and assign it to your previously created FILE object:

    pInput = fopen("filename," "r")

  9. 9

    Read in a line of the file using the following code:

    fgets(buf, sizeof(buf), pInput)

  10. 10

    Parse the CSV using the function "strtok". Create a new character string to point to the tokens, and initialise it with data from the line read in above:

    char *tok = strtok(buf, ",")

  11. 11

    Convert the received token into the appropriate data. Using the example line:

    1, "test", 3.45

    convert the data contained in "tok" to an integer using the following code:

    row.col1 = atoi(tok);

  12. 12

    For subsequent reads from the same line, pass "strtok" a NULL parameter instead of the buffer string you read in before:

    tok = strtok(NULL, ",")

    Then, convert the token to the appropriate data type. Using the example line


    The parsing code for a single line would be:

    char *tok = strtok(buf, ",");

    row.col1 = atoi(tok);

    tok = strtok(NULL, ",");

    row.col2 = tok;

    tok = strtok(NULL, ",");

    row.col3 = atof(tok);

  13. 13

    Do this for all of the entries on each line of the CSV. The function "strtok" will continue to provide the data between comma values until it runs out of data in the buffer, at which point it will return NULL. This will indicate that you have finished with the line.

Tips and warnings

  • Encapsulate your data conversion into another method to make the code more readable.
  • The "fgets" method will return NULL when the file has been completely read. Use this in a while loop to traverse the entire file.
  • The function "fopen" may return NULL if there was an error--make sure to check your file before using it.
  • In some cases, the tokens provided may not be of the expected format. Make sure to examine your data before blindly converting by checking against common error values (the empty string, NULL, etc).

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.