-2

I try to read several lines from a textfile and want to store the single items in a struct. The code looks like this:

std::string line;
struct _DomainTable_ line_of_file;
while (getline(infile_, line))
{
  std:: stringstream linebuffer(line);
  line_of_file.short_ = "";
  line_of_file.long_ = "";
  linebuffer >> line_of_file.id_ >> line_of_file.short_ << line_of_file.long_;
  domain_list_.push_back(line_of_file);
  it_++;
}

The single items in my text file are seperated by a blank. Unfortunately my program ignores the second blank between the second and the thrid column, so that the whole text after the "id_" is stored in "short_". Perhaps someone knows a better method to read a formatted file (such as scanf in C). I am workimg with C++ builder, and rather I don't want to use C anymore.

best regards and thank you very much Andreas Höhenberger

Hoeh
  • 45
  • 4
  • 1
    Please post example input and matching expected result – Johan Lundberg Jan 28 '18 at 20:30
  • this is one line of my file: 2 AZ Asbestzement. result should be: id_ = 2, short = AZ, long = Asbestzement. result is: id_ = 2, short = AZ Asbestzement, long = empty – Hoeh Jan 28 '18 at 21:37
  • Can you reproduce this if you write the input file by hand? I would just like to exclude encoding issues. – Johan Lundberg Jan 28 '18 at 21:43
  • Unrelated: Unlikely to cause you problems, but... `_DomainTable_` is a reserved identifier. [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Jan 28 '18 at 21:54
  • Tomorrow I will try to read a typical line from stdin. – Hoeh Jan 28 '18 at 22:06

1 Answers1

0

I solved my problem. There is a very good solution, which I found here:

https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c/1120224#1120224

String handling seems to be tricky under C++ too.

Hoeh
  • 45
  • 4