How can i read integers from non-initialized string?

I want to read a PPM file and record bytes as Glubyte* .
I should check comment lines. After checking the comment lines I get a line has two integer. I try to use stringstream but compiler says not initialized because i read string from file.

How can i read integers from non-initialized string?
It would be easier to answer if you shared this part of your code which gives the compiler error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
GLubyte* readPPM(string filename)
{
  GLubyte* byteArray;
  string text;
  int width, height, value;
  GLubyte temp;
  ifstream file;
  file.open(filename.c_str());
  getline(file,text);
  getline(file,text);
  while(text.at(0)!='#'){
    getline(file,text);
  }
  
  text >> width;
  text >> height;
  
  file >> value;
  
  // Operations

  return byteArray;
}


Input file format:

1
2
3
4
5
6
7
P3
#Some
# Comment
#  Lines
125 125
256
...


And the output:

 in function 'GLubyte* readPPM(std::string)' :
 error: no match for 'operator>>' in 'text >> width'

A std::string is not the same as a stringstream. You will have to create a std::stringstream (or std::istringstream) object that you use.
1
2
std::istringstream iss(text);
iss >> width >> height;
Now my code is:
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

GLubyte* readPPM(string filename)
{
  GLubyte* byteArray;
  string text;
  int width, height, value;
  GLubyte temp;
  ifstream file;
  file.open(filename.c_str());
  getline(file,text);
  getline(file,text);
  while(text.at(0)!='#'){
    getline(file,text);
  }
  
  istringstream iss(text);
  iss >> width >> height;

  file >> value;

// Operations

  return byteArray;
}


and the output is:


 in function 'GLubyte* readPPM(std::string)' :
error: variable  'std::istringstream iss'  has initializer but incomplete type
Last edited on
Have you included the stringstream header file?
#include <sstream>
thank everybody now it works
Topic archived. No new replies allowed.