From unformatted to formatted input

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
 
T

Thomas Matthews

TheDD said:
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:
#include <iostream>
#include <string>
using std::ios;
using std::istream;
using std::string;

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");

Undefined identifier "THROW"
Undefined identifier "InvalidFormat"

// Largeur
unsigned width;

unsigned char?
unsigned int?
unsigned short?
unsigned long?

width = atoi(nextToken(in).c_str());

Undefined identifer: nextToken

// in >> width;

unsigned height;

unsigned char?
unsigned int?
unsigned short?
unsigned long?

in >> height;

unsigned count = width * height;

unsigned char?
unsigned int?
unsigned short?
unsigned long?

texels.resize(count);

Undefined identifier: texels

texel_t val;

Undefined identifier: texel_t

while (in >> val && count > 0)
{
texels.push_back(val);
count--;
}
}
catch (std::exception & ex)
{
cerr << ex.what() << endl;
THROW(InvalidFormat, "file format exception");

Undefined identifer: THROW
Undefined identifer: InvalidFormat

}
catch (...)
{
THROW(InvalidFormat, "file format exception");

Undefined identifer: THROW
Undefined identifer: InvalidFormat

}
}

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

Which line is #67?
Where is the definition for the Image class?
Where are the definitions for the Undefined Identifiers
found above?


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
T

TheDD

the problem was coming from the loop, i changed:
while (in >> val && count > 0)
into
while (count > 0 && in >> val)

i've learned in school that unsigned = unsigned int and my compiler do not
complain
 
D

David Harmon

On Wed, 12 May 2004 12:52:39 GMT in comp.lang.c++, Thomas Matthews
unsigned char?
unsigned int?
unsigned short?
unsigned long?

Why do you list all of those things? Only one of them
is correct (and redundant.)
7.1.5.2 Simple type specifiers
 
J

John Harrison

TheDD said:
the problem was coming from the loop, i changed:
while (in >> val && count > 0)
into
while (count > 0 && in >> val)

i've learned in school that unsigned = unsigned int and my compiler do not
complain

The other problem with your code is

texels.resize(count);

I think you meant

texels.reserve(count);

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top