0

I'm reading with my c++ program csv file:

abc;def;ghi

10;;10

by this code:

    while(getline(in, str, '\n')){
      stringstream ss;
      while(getline(ss, str, ';')){
         line.add(str);
      }
    }

Where in is input file, str is string variable and line is my collection (like vector). But getline jumped over the empty string in csv file.

Can anyone help me to reading empty string, too?

Thanks :)

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740

1 Answers1

2

You've never initialized your stream!

Try this:

#include <string>   // for std::string and std::getline
#include <sstream>  // for std::istringstream

for (std::string jimbob; std::getline(in, jimbob); )
{
    std::istringstream marysue(jimbob);  // !
    for (std::string charlie; std::getline(marysue, charlie, ';'); )
    {
        line.add(charlie);
    }
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I've initialized it before by this: ifstream in ( inFileName ); if (in.fail()) return false; It's ok. But compiler doesnt like your code :( error: no matching function for call to ‘std::basic_istringstream::basic_istringstream(std::ifstream)' – Lukas Hamrla Mar 21 '13 at 21:37
  • You seem to be doing something completely different. There is no `ifstream` in my code at all. Are you misspelling `ln` as `in`? Let me rename the variables. – Kerrek SB Mar 21 '13 at 21:44
  • @LukasHamrla: I find that hard to believe. The code does [exactly what it should](http://ideone.com/k5JMCr). – Kerrek SB Mar 21 '13 at 21:52