473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

From unformatted to formatted input

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
4 2274
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

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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hugh Welford | last post by:
Hi ... My application saves variable length user-generated messages in an ACCESS memo field. ASP writes these messages correctly retaining CR LF to generate new paragraphs. (have checked this in...
14
by: StumpY | last post by:
HI, I have set up a page with a form which appends data to a .csv file on my asp server, this is to enable a limited number of users to add news threads to this file, which contains the data in...
2
by: Mitch | last post by:
Hi all, I hope this is the right place for this post, apologies if it is not. I am trying to write a program that creates an input file for a dispersion model. The model is expecting a Fortran...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
3
by: Bob | last post by:
I had an app in VB6 that I'm "upgrading" <GGGG> to Vb.net 2005. In Vb6 I had rtf text in a field in a Sql server 2000 field and it would show up as formatted RTF in my richtextbox. In vs.net that...
3
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX...
6
by: geegeegeegee | last post by:
Hi All, I have come across a difficult problem to do with extracting UniCode characters from RTF strings. A detailed description of my problem is below, if anyone could help, it would be much...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.