Re: From unformatted to formatted input

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 05/12/04


Date: Wed, 12 May 2004 12:52:39 GMT

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