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