3 Replies - 981 Views - Last Post: 25 January 2013 - 04:54 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, Ostream/Istream

Posted 24 January 2013 - 04:10 PM

Hi,
I have a csv file that looks like:

Anna
1, 2, 3, 4
Bert
4, 5, 6, 9.5
Iga
5, 7, 7, 10


I have the following Header:

##include <fstream>
##include <sstream>
##include <vector>
##include <iterator>
#include <alorithm>


class Class_Child 
{
private:
std::string name;
std::vector<double> note;

public:
    bool operator <(Class_Child const&other) const {
    return name< other.name;
}
//  Insertion operator: write data out
friend std::ostream &operator<<(std::ostream &os, Class_Child const &child)
{
os << child.name<<'\n';
os << child.note.size();
for (int i=0; i<child.note.size; ++i) 
os << child.note[i];
   return(os);
}
// Extraction operator: Read data in
friend std::istream &operator>>(std::istream &stream, Class_Child &child)
{
 // read the entire line into a string
std::getline(stream, child.name);

//Read the line of scores and convert them into double
std:: string scores;
std::getline(stream, scores);
std::stringstream scorestream(scores);
std::copy (std::istream_iterator<double>(scorestream),
std::istream_iterator<double>(),
std::back_inserter(child.note));
return stream;
}
};
 

Main

int main()
string Filename;          
cout << "Give name of input file: "; // Child.csv
cin >> Filename;
  
std::ifstream dataFile(Filename.c_str());
    
// Declare a vestor of children
std::vector<Class_Child> children;
      if (! dataFile)
    {
        cout << "Error opening input file" << endl;
        return -1;
    }
       
        // Read characters in from file

     //Copy all children into vector
     std::copy(std::istream_iterator<Class_Child>(dataFile),
             std::istream_iterator<Class_Child>(),
             std::back_inserter(children));
   dataFile.close();
  
   
      std::copy(children.begin(), children.end(),std::ostream_iterator<Class_Child>(std::cout,"\n"));
   
return 0;
}


The Output is:
Anna;;
Bert;;
Iga;;

Problem is after read the csv file, the values of notes are not displayed. Why? I suppsoe there is something wrong with the ostream function. So any suggestions to correct my code?

This post has been edited by jimblumberg: 24 January 2013 - 09:18 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, Ostream/Istream

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Read CSV file, Ostream/Istream

Posted 24 January 2013 - 05:37 PM

You're not reading only doubles from the stringstream, you want to read a double and a character (comma or newline).

std::copy (std::istream_iterator<double>(scorestream),
std::istream_iterator<double>(),
std::back_inserter(child.note));




size is a function.

for (int i=0; i<child.note.size; ++i) 


This post has been edited by #define: 24 January 2013 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 caa  Icon User is offline

  • New D.I.C Head

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

Re: Read CSV file, Ostream/Istream

Posted 25 January 2013 - 03:32 AM

Do you mean it should work?


//Read the line of scores and convert them into double  
std:: string scores;  
std::getline(stream, scores);  

////// convert ',' to ' '
std::size_t pos=0;
double d=0.0

while (pos<scores.size())
if ((pos=scores.find_first_of(,),pos)!=std::string::npos)
scores[pos]='';
///////////////////
std::stringstream scorestream(scores);  
std::copy (std::istream_iterator<double>(scorestream),  
std::istream_iterator<double>(),  

std::back_inserter(child.note));

This post has been edited by jimblumberg: 25 January 2013 - 07:19 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Read CSV file, Ostream/Istream

Posted 25 January 2013 - 04:54 PM

View Postcaa, on 25 January 2013 - 12:32 PM, said:

Do you mean it should work?


I mean it could work using the copy algorithm.

Since the doubles are separated by commas they can't be read by copy.

The only way I can think to do it is to create another class for the values, which can discard the commas.

class Note
{
  public:
    double no;

  // ...
};


class Class_Child
{
  private:
    std::string name;
    std::vector<Note> notes;

  // ...
};


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1