Read variable number of fields in a line

Hello, I'm trying to read data from a file of 6 lines, storing the values of the fields found in each line in 6 different vectors, knowing that this number of fields is variable. Therefore, I assumed I needed a condition to check when the end line is reached, but I'm not sure I've found anything that actually works.

My idea was to count the number of spaces between fields in a line and, then, to read this amount of times + 1 from it. Unfortunately, in doing so I end up using getline() together with tellg() and seekg(), which cause problems by cutting off some characters. I tried different times, but I still don't quite know why this happens nor how I would go to solve it. Any suggestion on the correct implementation of this particular approach or an analogous one in respect to the original problem is greatly appreciated. Here's what I tried to do for the first line, same things for the others:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
         begin = myfile.tellg();
         getline (myfile, line);
         for (char c : line) {

             if (c == '\t') {

                n++;

             }

         }

         myfile.seekg(begin);
	 for (int i = 0; i < (n+1); i++) {

             if (i < n) {

	        getline (myfile, DATA, '\t');

             }

             else {

	        getline (myfile, DATA, '\n');

             }

             (*pkmn).setITEM(DATA);

	 }

	 n = 0;
Read a line into a string. Create a std::istringstream from the string. Read values from the istream into a vector.

Repeat the operation with each line.
Thank you, that was exactly what I was looking for. Can I ask you one more thing? I had to use every time a different istringstream variable, is there a way to reuse the same, instead? I found out I should use mystring.str("") and clear.mystring(), but still when I try to open the line again it gives me the following error:
no match for call to '(std::istringstream {aka std::basic_istringstream<char>}) (std::string&)'|


getline(myfile, line);
istringstream mystring(line);
//do stuff
mystring.str("");
clear.mystring();

getline(myfile, line);
mystring(line);// error
//do stuff
mystring.str("");
clear.mystring();

...



Thank you again.
It seems possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::istringstream

int main () {
  std::istringstream iss;
  std::string strvalues = "32 240 2 1450";

  iss.str (strvalues);

  int val;
  while ( iss >> val )
  {
    std::cout << val << '\n';
  }
  std::cout << "Finished writing the numbers in: ";
  std::cout << iss.str() << '\n';

  iss.clear(); // clear the fail flag
  iss.str( "7 42" ); // set new content
  while ( iss >> val )
  {
    std::cout << val << '\n';
  }
  std::cout << "Finished writing the numbers in: ";
  std::cout << iss.str() << '\n';

  return 0;
}
Topic archived. No new replies allowed.