Help with CSV!

This is a discussion on Help with CSV! within the C++ Programming forums, part of the General Programming Boards category; Hi, please can someone explain how I would take a text file containing comma-separated variables and put it into an ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Cape Town, South Africa
    Posts
    8

    Help with CSV!

    Hi, please can someone explain how I would take a text file containing comma-separated variables and put it into an array in C++ so that I can easily work with the data. The CSV file has 3000 rows and about 5 columns, and I'd like to keep the array with the same number of rows and columns.

    I'm new to C++, so I hope that this question makes sense. If not could someone please explain what would be the best way of getting this data into C++ so that I can work with it easily?

    Thanks in advance for your help!

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    fscanf is one partial solution, although that is more of a C method than a C++ one.

    If you want to use C++ tools, use an ifstream to open the file and then use getline to read in each line. You can then use a stringstream with getline to separate each value in each line. The benefit of this is that it will make it easier to handle bad input.

    For the array, it sounds like you need an array of objects, so I would make a struct or class that holds the types of data stored in the columns. Then, use a vector instead of an array to hold each row.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Location
    Cape Town, South Africa
    Posts
    8
    Thanks for that, I'm going to get started with it right away. Just one question, what would the elements of the vectors be in this case? Would each element of the vector be one site (so that I have one vector with 3000 elements)? or would I have multiple vectors containing 5 elements (latitude, longitude, slope, aspect, altitude, all of which are doubles)?

    Sorry if this seems like an obvious question, I'm just struggling a bit to understand how vectors compare to arrays.

    Thanks again,

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you know structs or classes, make each element of the vector an instance of that class. You could make it a vector of vectors of doubles, but it makes more sense to combine the five doubles into a struct and just have a single vector.

    Think of a vector as the same as an array. You could do the same thing with an array. Either a single array of your struct or class, or a two dimensional array of doubles. If you know you will always have exactly 3000 rows, then an array might even be a better choice. A vector is good for when you're not sure when you compile how big the array will be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner C programmer - My CSV reader
    By spadez in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 08:46 PM
  2. CSV Parsing
    By spadez in forum C++ Programming
    Replies: 13
    Last Post: 02-04-2009, 09:28 AM
  3. read in csv file
    By gums in forum C Programming
    Replies: 5
    Last Post: 05-10-2007, 07:38 AM
  4. DBF to CSV in c#
    By TheMajorRager in forum C# Programming
    Replies: 0
    Last Post: 10-04-2005, 11:23 AM
  5. how to read csv file ?
    By kosong in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 07:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21