Thread: Reading in specific columns from csv file - C++

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    6

    Reading in specific columns from csv file - C++

    I have a csv file with several columns and rows. It is quite large, so for testing purpose I have a test file like this:

    Code:
    WAST,DP,Dta,Dts,EV,QFE,QFF,QNH,RF,RH,S,SR,ST1,ST2,ST3,ST4,Sx,T
    31/03/2016 09:00,14.6,175,17,0,1013.4,1016.9,1017,0,68.2,6,512,22.7,24.1,25.5,26.1,8,20.74
    What I need to do, is read the date and time from column 1 called 'WAST', and the float values from the column 'S'. So far, I've created a main program that reads from a test file that looks like this:

    Code:
    31/03/2016 09:00,14.6
    Main program:
    Code:
    using namespace std;
    
    
    typedef struct {
    
    
            Date d;
            Time t;
            float speed;
    
    
    }WindLogType;
    
    
    int main()
    {
    
    
    Date dTest;
    Time tTest;
    float speedtest = 52.5;
    
    
    Vector<WindLogType> windlog;
    
    
    
    
    ifstream infile("testinput.csv");
    
    
    if(!infile){
    
    
        cout << "File not found.";
    
    
        return -1;
    
    
    };
    
    
    WindLogType windlog2;
    
    
    //int i = 0;
    
    
    while(!infile.eof()){
    
    
        infile >> windlog2.d >> windlog2.t >> windlog2.speed;
    
    
    
    
        windlog.add(windlog2);
    
    
    }
    
    
    for(int i = 0; i < windlog.size(); i++){
    
    
        cout << windlog[i].d << " " << windlog[i].t << " Speed: " << windlog[i].speed << endl;
    
    
    }
    
    
    
    
    infile.close();
    
    
    return 0;
    
    
    }
    My Date and Time classes use getline to get the correct delimiters and successfully separate the Date and Time from one line, so that seems okay. This program works fine with the second test file. Now how am I supposed to make it so that I read in only the two columns that I want?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to parse the CSV file. My guess is that you can assume that none of the CSV fields contain a ',' or a newline. If so, then parsing is simple: just read line by line using std::getline into a std::string, then parse the string into tokens with ',' as the delimiter. Extract only the tokens corresponding to the WAST and S headers (if the headers are actually in the file, then you need to parse them first to find out their indices).

    Once you have these tokens, you just construct your Date and Time objects with them (i.e., you don't need your current "getline to get the correct delimiters and successfully separate the Date and Time from one line").
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Be aware that CSV files can use double quotes (") to specify strings. For example:
    Code:
    "String, with comma",10,3.14
    So, tokenizing, using ',' as separator must be done with care.
    There are other things to worry about... I recommend using libcsv..
    Last edited by flp1969; 05-02-2020 at 07:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-04-2014, 02:23 PM
  2. Reading from file with specific widths.
    By looza in forum C Programming
    Replies: 2
    Last Post: 11-03-2011, 12:56 PM
  3. Replies: 6
    Last Post: 08-04-2003, 10:57 AM
  4. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  5. Need help reading in specific type of file
    By Natase in forum C Programming
    Replies: 4
    Last Post: 09-12-2001, 08:02 PM

Tags for this Thread