By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

From unformatted to formatted input

P: n/a
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

--
TheDD
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
TheDD wrote:
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

Jul 22 '05 #2

P: n/a

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

--
TheDD
Jul 22 '05 #3

P: n/a
On Wed, 12 May 2004 12:52:39 GMT in comp.lang.c++, Thomas Matthews
<Th****************************@sbcglobal.net> wrote,
unsigned width;


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

Jul 22 '05 #4

P: n/a

"TheDD" <pa*********@pas.de.spam.fr> wrote in message
news:c7**********@news-reader3.wanadoo.fr...

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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.