How to use stringstream with arduino ide

Is there any alternative method to use stringstream with Arduino IDE?
I have this code and I need to use it with Arduino.

string newlin;
getline(ifs,newlin);    // ifs is a file
int N1;
stringstream t(newlin);
t >> N1;


Update :: 
In this code I need to read pgm image from sd card. The commented lines represent the code in cpp, and it was work (as a cpp code) without any problem. I converted some functions the deal with files in cpp to can work on Arduino IDE but the problem I can't get the size of image correctly. I convert this line:

 //streampos old = ifs.tellg();
to
auto old = ifs.position(); 
and this
 // getline(ifs,newlin);`
to
 newlin = ifs.read();
 
But I do not know how to deal with these lines in correct way.
 
stringstream t(newlin);
t >> N1;
 
The output of the cpp code is :

512 : R - Height
512 : C - Width 
255 : Maximum Value (bits)

And the output of Arduino code is:

R = -1579468223 
C = 476207462 
bits = -436054396 


#include
#include
#include
#include
#include
#include"Lib.h"
#include<SD.h>

using namespace std;

vector < vector > read_pgm_image(char filnam[], char filemod[], char comment[], int & R, int & C, int & bits) /reads PGM images/
{
vector < vector > img;
File ifs;
File iftest;
string newlin;
//iftest.open(filnam,ios::in);
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Failed to mount card read pgm");
}
iftest = SD.open(filnam, FILE_READ);
if (!iftest) {
Serial.println("Opening file to read failed read pgm");
}
int header_size = 3;
for (int i = 0; i < 4; i++)
{

//getline(iftest,newlin);
newlin = iftest.read();

// Serial.printf("newline = %s",newlin);
if(newlin.data()[0] == '#')
{
header_size = 4;
break;
}

}

iftest.close();

// ifs.open(filnam, ios::in);

if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Failed to mount card read pgm");
}
ifs = SD.open(filnam, FILE_READ);
if (!ifs) {
Serial.println("Opening file to read failed read pgm");
}
bool h = 0;
int i = 0;

for (int i = 0; i < header_size; i++)
{
  //  auto old = ifs.tellg();
    auto old = ifs.position();
   // getline(ifs,newlin); 
    newlin = ifs.read();
    int N1;
    stringstream t(newlin);
    t >> N1;
         
    if(strcmp(newlin.data(),"P2") == 0 | strcmp(newlin.data(),"P5") == 0 )
    {
        strcpy(filemod,newlin.data());
    }
    else if(newlin.data()[0] == '#')
    {
        strcpy(comment,newlin.data());
    }
    else if(t)
    {
        
        int NN;
        t >> NN;
        if (t)
        {
            C = N1;
            R = NN;
        }
        else
        {
            bits = N1;
        }
    }
    else
    {
       // cout << "\n Overread into bits!! Going back to bit line!!! \n";
      //  ifs.seekg(old);
        break;
        
    }
    
}

ifs.close();

File newp;
//newp.open(filnam, ios::in|ios::binary);

if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Failed to mount card read pgm");
}
newp = SD.open(filnam, FILE_READ);
if (!newp) {
Serial.println("Opening file to read failed read pgm");
}
char tt;
//newp >> tt;
tt = newp.read();
for (int i = 0; i < header_size; i++)
{
// getline(newp,newlin);
newlin = newp.read();
}
//cout << R << " : R - Height <- \n";
//cout << C<< " : C - Width <- \n";
//cout << bits<< " : Maximum Value <- \n";
Serial.printf("R = %d \n" ,R);
Serial.printf("C = %d \n" ,C);
Serial.printf("bits = %d \n" ,bits);

vector zvc(C);
fill(zvc.begin(),zvc.end(),0);

for(int i = 0; i < R; i++)
{
img.push_back(zvc);
}
int k = 0,l = 0;
long cnt = 0;

for(k = 0; k < R; k++)
{
    for(l = 0; l < C; l++)
    {

        if(newp.read() == 1)
        {
        break;    
        }
        
    int val = 0;
    int p = 7;
    char c11;
   // newp.get(c11);
    
    while(p >=0)
    {
        
        val += (((c11 >> p) & 1) ) * pow(2,p);
        p--;
    }
    
    img[k][l] = val;
    
    cnt++;
    
    }        
}
newp.close();
return img;

}
void setup() {

Serial.begin(9600);
while (!Serial){};

char filt[100];
char comment[1000] = " ";
int R,C,bits;
vector < vector > img;
int vals1[3];
img = read_pgm_image("lena.pgm",filt,comment,R,C,bits); /read the PGM image/
Serial.println(R);
Serial.println(C);
Serial.println(bits

Please explain what that code snippet does

This is a part of cpp code that use to read image.

OK, but what does it actually do ?

I'll update the question to share all the code.

std::stringstream is a class in the C++ Standard Library (part of the <sstream> header) that allows you to perform formatted input and output operations on strings.

It's used often for converting between strings and other data types

t >> N1;

will convert the line into an int

@mercala_eng if you insist on using the String class, then parseInt() might be what you are looking for

String newlin;
// Read a line from your file into the 'newlin' variable
•••

// now extract the integer (actually a long)
long N1 = newlin.toInt();

or you could possibly call directly parseInt() on the file.

Please edit your post and add code tags where appropriate

I got on this error:

strcpy(comment,newlin.data());
'class String' has no member named 'data'

How should anybody analyse and explain if you post just such a small snippet of code.
the function strcopy() expects two pointers to zero-terminated c_strings

You haven't provided the declaration of your variable named "newlin"
without the declaration it is impossible to analyse what exactly is the bug.

Anyway you asked for a detail.
What is the final and overall purpose of using the code that you have posted?

If you stay on the level of
"I need to use this code" which is just a detail
the detail answer is:

'class String' has no member named 'data'
end of message

If you use the Arduino String class or the standard string class then you access to the underlying cString using newlin.c_str()

https://cplusplus.com/reference/string/string/c_str/

There is no data function

Well read the code it’s right there in the first post

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.