reading csv files to a 2D string array in C++
Results 1 to 3 of 3

Thread: reading csv files to a 2D string array in C++

  1. #1
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Beverwijk Netherlands

    reading csv files to a 2D string array in C++

    Hey all,

    I'm working on a data concistency checking program,
    Now all that stuff is done in VBA(script) in Excel, and I'm trying to write a good C++ app to replace that.

    I'm trying to write a good (safe) function to read a CSV (comma seperated values) file into a 2D array (suggestions on doing something totaly different like linked lists are welcome too)

    I already wrote a quick and dirty readout sceme, that does it's work but can cause strange mallfunctions.

    the CSV files are rather simple..



    but also


    is possible..

    so array's are not realy the option of choice..

    the biggest problem is that there are no ( google / ao-search / etc.. ) good cut-n-paste pieces of code out there..

    if anyone has any idea's, please post them, they could only be helpfull

    thanks in advance,


  2. #2
    Senior Member
    Join Date
    Jul 2002
    Done in VBA(script) in Excel, eh? I made a C program that mimics a (simplified) Lotus 1-2-3 for DOS for my assignment years back. I remember the key thing was to use union. So, continue using array -- of unions.

    A union is a user-defined data type that can hold values of different types at different times. It is similar to a structure except that all of its members start at the same location in memory. A union variable can contain only one of its members at a time. The size of the union is at least the size of the largest member.
    I'm assuming you have no difficulties in reading and parsing data from the CSV file, also, you know how to differentiate the data type (string vs float). Then here is the pseudocode,

    #define MAX_CELLS 1000

    union DATA
    string s;
    float f;

    enum TYPE { STRING, FLOAT };

    struct CELL
    enum TYPE type;
    DATA data;

    CELL cells[MAX_CELLS];

    while (acell = <insert file reading and parsing here>)
    celltype = <insert type checking routine here>;
    if (celltype == STRING)
    cells[i].type = STRING;
    cells[i].data.s = acell;
    else if (celltype == FLOAT)
    cells[i].type = FLOAT;
    cells[i].data.f = acell;

    For your reference. Hope this helps.

    Peace always,
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds

  3. #3
    Antionline Herpetologist
    Join Date
    Aug 2001
    Darn! you beat me to it. I was just going to suggest using an union. However, you might want to use the STL class vector instead of an array. So you could make a vector of union objects rather than an array type union.
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts