1 Replies - 1568 Views - Last Post: 29 January 2013 - 10:07 PM Rate Topic: -----

#1 caa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-January 13

Read CSV file, Vector<Vector

Posted 29 January 2013 - 05:10 PM

I have to read the following CSVdata into vector of vectors

Anna
2,4, 4
xx, xx, xx, xx
100


I have the following function to red the data in


Header

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>


std::vector<std::vector<std::string> > readCSV1(std::fstream& inn)
{
   
   std::vector<vector<std::string> > contents;
    std::string s;
    size_t lastpos, curpos;

    while(std::getline(inn, s))
    {
   contents.push_back( vector<std::string>());
    for (lastpos=curpos=0; curpos!=std::string::npos; lastpos=curpos+1)
    {
      curpos = s.find(',', lastpos);
      if (curpos != std::string::npos)
        contents.back().push_back(s.substr(lastpos, curpos-lastpos));
      else
        contents.back().push_back(s.substr(lastpos));
    }
    }
    return contents;
} 


int main()
    // read the file
  string Filename7;          
  cout << "Give name of input file Data.csv: ";
cin >> Filename7;
     std::fstream dataFile7(Filename7.c_str());
    std::vector<std::vector<std::string> > csv;
    csv= readCSV1(dataFile7);
for (size_t i=0; i < csv.size(); i++)
  {
    for (size_t j=0; j < csv[i].size(); j++)
    {
        cout << csv[i][j] << "\t";
    }
    std::cout << endl;
  }
    cout<<csv[0][0]<<endl;
        
     }



I do not really get the output in the matrix form, the lines are not parsed into strings.
The size of my vector is [7][0], instead of [2][3] How should I modify the code to parse the strings in the lines into vectors?

This post has been edited by jimblumberg: 29 January 2013 - 10:34 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Read CSV file, Vector<Vector

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,618
  • Joined: 19-February 09

Re: Read CSV file, Vector<Vector

Posted 29 January 2013 - 10:07 PM

View Postcaa, on 30 January 2013 - 02:10 AM, said:

I have to read the following CSVdata into vector of vectors


Often CSV data is of a record type and usually structs or classes are used. A list would then be a vector of structs.
Is it a vector of vectors what you want?


View Postcaa, on 30 January 2013 - 02:10 AM, said:

Anna
2,4, 4
xx, xx, xx, xx
100


Is this one record? Is it all on one line? Is it just a sample?



View Postcaa, on 30 January 2013 - 02:10 AM, said:

How should I modify the code to parse the strings in the lines into vectors?


For data followed by a comma you can use getline (string) with a delimiter (comma).

vector<std::string> record;

// get name 
// assumption = on one line
while( std::getline(inn, str) )
{
  // push name 
  record.push_back(str);
  // check data
  std::cout << str << endl;

  // use delimiter
  std::getline(inn, str, ',');
  record.push_back(str);
  // check data
  std::cout << str << endl;

  // ...

  // store vector
  contents.push_back(record);
}  




.

This post has been edited by #define: 29 January 2013 - 11:28 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1