T
TheDD
Hello all,
i'm having a problem with input. My aim is to read a pbm file wich looks
like:
$ head file.pbm
P1
# Created by Paint Shop Pro 7
3510 2550
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Here is my code:
void
Image::loadFromPbm(istream & in)
{
try
{
in.exceptions(ios::badbit | ios::failbit);
// Vérifie le "magic"
char magic[3];
in.read(magic, 2);
magic[2] = '\0';
if (strcmp(magic, "P1"))
THROW(InvalidFormat, "bad magic");
// Largeur
unsigned width;
width = atoi(nextToken(in).c_str());
// in >> width;
unsigned height;
in >> height;
unsigned count = width * height;
texels.resize(count);
texel_t val;
while (in >> val && count > 0)
{
texels.push_back(val);
count--;
}
}
catch (std::exception & ex)
{
cerr << ex.what() << endl;
THROW(InvalidFormat, "file format exception");
}
catch (...)
{
THROW(InvalidFormat, "file format exception");
}
}
string
Image::nextToken(istream & in)
{
string token;
bool ok;
do
{
in >> token;
cerr << "TOKEN = " << token << endl;
ok = (token[0] == '#');
if (!ok)
{
// ignore la fin de la ligne
in.ignore(numeric_limits<int>::max(), '\n');
}
cerr << "eof = " << in.eof() << endl;
cerr << "rdstate = " << in.rdstate() << endl;
} while (!ok);
return token;
}
And the output i'm having:
TOKEN = #
eof = 0
rdstate = 0
basic_ios::clear(iostate) caused exception
InvalidFormat exception with:
* file = image.cc
* line = 67
* msg = file format exception
I don't understand why it doesn't work. rdstate == 0, so clear() should
work...
Any help appreciated, TIA
i'm having a problem with input. My aim is to read a pbm file wich looks
like:
$ head file.pbm
P1
# Created by Paint Shop Pro 7
3510 2550
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Here is my code:
void
Image::loadFromPbm(istream & in)
{
try
{
in.exceptions(ios::badbit | ios::failbit);
// Vérifie le "magic"
char magic[3];
in.read(magic, 2);
magic[2] = '\0';
if (strcmp(magic, "P1"))
THROW(InvalidFormat, "bad magic");
// Largeur
unsigned width;
width = atoi(nextToken(in).c_str());
// in >> width;
unsigned height;
in >> height;
unsigned count = width * height;
texels.resize(count);
texel_t val;
while (in >> val && count > 0)
{
texels.push_back(val);
count--;
}
}
catch (std::exception & ex)
{
cerr << ex.what() << endl;
THROW(InvalidFormat, "file format exception");
}
catch (...)
{
THROW(InvalidFormat, "file format exception");
}
}
string
Image::nextToken(istream & in)
{
string token;
bool ok;
do
{
in >> token;
cerr << "TOKEN = " << token << endl;
ok = (token[0] == '#');
if (!ok)
{
// ignore la fin de la ligne
in.ignore(numeric_limits<int>::max(), '\n');
}
cerr << "eof = " << in.eof() << endl;
cerr << "rdstate = " << in.rdstate() << endl;
} while (!ok);
return token;
}
And the output i'm having:
TOKEN = #
eof = 0
rdstate = 0
basic_ios::clear(iostate) caused exception
InvalidFormat exception with:
* file = image.cc
* line = 67
* msg = file format exception
I don't understand why it doesn't work. rdstate == 0, so clear() should
work...
Any help appreciated, TIA