472,805 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Formatted input interpretation

Hello,

i'm developping some function to read pbm image files. Somewhere in my code
i have:

texel_t val;

while (in >> val)
{
...
}

the problem is that since the "real" type of texel_t is unsigned char, so
the my input is 48 instead of 0 (a character is read instead of a small
number).

I was hopping for some kind of manipulator to force the interpretation to "a
_number_ coded on a char" but i didn't find it.

What if the texel_t type was a template parameter, how can i force the
interpretation, if possible?

Right now, i'm using a temporary int value, but it's a hack :(

TIA for your time

--
TheDD
Jul 22 '05 #1
7 1450
"TheDD" <pa*********@pas.de.spam.fr> wrote...
i'm developping some function to read pbm image files. Somewhere in my code i have:

texel_t val;

while (in >> val)
{
...
}

the problem is that since the "real" type of texel_t is unsigned char, so
the my input is 48 instead of 0 (a character is read instead of a small
number).

I was hopping for some kind of manipulator to force the interpretation to "a _number_ coded on a char" but i didn't find it.
If you're reading one digit, then simply subtract '0' from the result...
What if the texel_t type was a template parameter, how can i force the
interpretation, if possible?
You'd need to specialise your functions for 'char' and others and make
the behaviour differ depending on the type.
Right now, i'm using a temporary int value, but it's a hack :(


No, it's not, really.

Victor
Jul 22 '05 #2
"TheDD" <pa*********@pas.de.spam.fr> wrote in message
news:c85gme$4be$1@news-
texel_t val;

while (in >> val)
{
...
}

the problem is that since the "real" type of texel_t is unsigned char, so
the my input is 48 instead of 0 (a character is read instead of a small
number).


If the stream is "25" what will you read. Will these be 2 texel_t objects
with values 2 and 5? Or will it be one with value "25"? And what will a
stream of "257" read into? Will it be 3 texel_t objects? Or will it be one
which puts the stream into a fail state because of an overflow?
Jul 22 '05 #3
Siemel Naran wrote:
texel_t val;

while (in >> val)
{
...
}

the problem is that since the "real" type of texel_t is unsigned char, so
the my input is 48 instead of 0 (a character is read instead of a small
number).


If the stream is "25" what will you read. Will these be 2 texel_t objects
with values 2 and 5? Or will it be one with value "25"? And what will a
stream of "257" read into? Will it be 3 texel_t objects? Or will it be
one which puts the stream into a fail state because of an overflow?


What i would like is:

"25" -> 1 texel_t with value 25
"257" -> fail state

I've forget to say that in pbm (P1) files, the color is white (0) or black
(1). But the question is good for future extensions (and c++ knowledge ;).

--
TheDD
Jul 22 '05 #4
"TheDD" <pa*********@pas.de.spam.fr> wrote in message
news:c8631d$bai$1@news-> Siemel Naran wrote:
texel_t val;
while (in >> val)
If the stream is "25" what will you read. Will these be 2 texel_t objects with values 2 and 5? Or will it be one with value "25"? And what will a stream of "257" read into? Will it be 3 texel_t objects? Or will it be
one which puts the stream into a fail state because of an overflow?


What i would like is:

"25" -> 1 texel_t with value 25
"257" -> fail state

I've forget to say that in pbm (P1) files, the color is white (0) or black
(1). But the question is good for future extensions (and c++ knowledge ;).


Easiest way is to read an unsigned integer, check if it is more than 255 and
if so set the failbit, then cast to an unsigned char.

To make it clear make texexl_t a class so that you can overload operator>>
for it, and ideally you'll suffer no performance overhead because the
compiler would optimize texcel_t as if it were an unsigned integer.

class exel_t {
public:
excel_t() { }
unsigned char value() const { return d_value; }
private:
unsigned char d_value;
};

inline
std::istream& operator<<(std::istream& i, exel& e) {
unsigned value;
i >> value;
if (i >= 1<CHAR_BIT) e.clear(ios::failbit);
else e.d_value = (unsigned char)(value);
return i;
}

But the use of istream >> int is still quite expensive because it handles
sentries, does locale dependent formatting, etc. There are alternative
solutions available. One is to use fscanf. Another is to get the
underlying streambuf with i.rdbuf() and parse the chars yourself manually.
There was a post on this NG in the last few days about how to convert an
array of chars into an integer.
Jul 22 '05 #5
Siemel Naran wrote:
>> texel_t val;
>> while (in >> val) > If the stream is "25" what will you read. Will these be 2 texel_t objects > with values 2 and 5? Or will it be one with value "25"? And what will a > stream of "257" read into? Will it be 3 texel_t objects? Or will it
> be one which puts the stream into a fail state because of an overflow?
What i would like is:

"25" -> 1 texel_t with value 25
"257" -> fail state

I've forget to say that in pbm (P1) files, the color is white (0) or
black (1). But the question is good for future extensions (and c++
knowledge ;).


Easiest way is to read an unsigned integer, check if it is more than 255
and if so set the failbit, then cast to an unsigned char.


Sounds great :)
To make it clear make texexl_t a class so that you can overload operator>>
hu... a lot less
for it, and ideally you'll suffer no performance overhead because the
compiler would optimize texcel_t as if it were an unsigned integer.

class exel_t {
public:
excel_t() { }
unsigned char value() const { return d_value; }
private:
unsigned char d_value;
};

inline
std::istream& operator<<(std::istream& i, exel& e) {
unsigned value;
i >> value;
if (i >= 1<CHAR_BIT) e.clear(ios::failbit);
else e.d_value = (unsigned char)(value);
return i;
}
it looks like a rocket launcher to kill the bug...
But the use of istream >> int is still quite expensive because it handles
sentries, does locale dependent formatting, etc. There are alternative
solutions available. One is to use fscanf. Another is to get the
underlying streambuf with i.rdbuf() and parse the chars yourself manually.
Well i would like a way to do it transparently, i was hopping for a self
made manipulator but it's not possible.

I think it's a lack in c++.
There was a post on this NG in the last few days about how to convert an
array of chars into an integer.


Thx i know how to do that :)

--
TheDD
Jul 22 '05 #6
"TheDD" <pa*********@pas.de.spam.fr> wrote in message
news:c8649a$6eb$1@news-
Siemel Naran wrote:
class exel_t {
public:
excel_t() { }
unsigned char value() const { return d_value; }
private:
unsigned char d_value;
};

inline
std::istream& operator<<(std::istream& i, exel& e) {
unsigned value;
i >> value;
if (i >= 1<CHAR_BIT) e.clear(ios::failbit);
else e.d_value = (unsigned char)(value);
return i;
}


it looks like a rocket launcher to kill the bug...


Fair enough, but maybe it's because the style is new. You could do

inline
void read_excel(std::istream& i, exel& e) {
unsigned value;
i >> value;
if (i >= 1<CHAR_BIT) e.clear(ios::failbit);
else e = (unsigned char)(value);
}

But the class version is only 8 lines more.

But the use of istream >> int is still quite expensive because it handles sentries, does locale dependent formatting, etc. There are alternative
solutions available. One is to use fscanf. Another is to get the
underlying streambuf with i.rdbuf() and parse the chars yourself

manually.
Well i would like a way to do it transparently, i was hopping for a self
made manipulator but it's not possible.


What do you mean by "transparently"?
Jul 22 '05 #7
On Sat, 15 May 2004 22:16:02 GMT, Siemel Naran wrote:
class exel_t {
public:
excel_t() { }
unsigned char value() const { return d_value; }
private:
unsigned char d_value;
};

inline
std::istream& operator<<(std::istream& i, exel& e) {
unsigned value;
i >> value;
if (i >= 1<CHAR_BIT) e.clear(ios::failbit);
else e.d_value = (unsigned char)(value);
return i;
}
it looks like a rocket launcher to kill the bug...


Fair enough, but maybe it's because the style is new. You could do


the style is new?
inline
void read_excel(std::istream& i, exel& e) {
unsigned value;
i >> value;
if (i >= 1<CHAR_BIT) e.clear(ios::failbit);
else e = (unsigned char)(value);
}

But the class version is only 8 lines more.


true, but i meaned that it's just "hidding the hack behind a
function". I thank you for your code anyway.
But the use of istream >> int is still quite expensive because it handles sentries, does locale dependent formatting, etc. There are alternative
solutions available. One is to use fscanf. Another is to get the
underlying streambuf with i.rdbuf() and parse the chars yourself

manually.

Well i would like a way to do it transparently, i was hopping for a self
made manipulator but it's not possible.


What do you mean by "transparently"?


well, with, 1 or 2 tokens, and without a need of specializing the
template if it was one (it will probably be).Something "classy" ;)

--
TheDD
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Craig Thomson | last post by:
Do people generally try and output nicely formatted html from their scripts? I have been trying to, with variable indenting for tables etc. It makes it easier to see the html and perhaps debug...
4
by: Fabian | last post by:
I have a three tier nested array, used to define a map for a javascript game, and can be edited within the web page. Is there a way I can generate a visible copy of this array that I can then c&p...
4
by: frogman042 | last post by:
My daughter is playing around trying to learn JavaScript and she wrote a small program that prints out a message in increasing and decreasing font size and color changes. She is using document...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
3
by: funkyMonkey | last post by:
I'm binding date fields from entity objects and formatting the output in the text box in short date format. Code: BindDateField(Me.txtCheckIn, "Text", reservation.BookingDetail, "CheckIn")...
1
by: Tarun Mistry | last post by:
Hi everyone, is it possible to return the formatted interpretation of an XmlDocument object? Using an XmlWriter object I can write a formatted document to a file, however I want to store it...
6
by: bfowlkes | last post by:
Hello, I am trying to parse two pre-formatted text files and write them to a different files formatted in a different way. The story about this is I was hired along with about 20 other people...
2
by: dkk | last post by:
I am new to C programming. I need to read data from a formatted input text file (column-based), for example, "12345abcde678", I want to parse it into "123", "45", "ab", "cde", "678", and write them...
2
by: David J Birnbaum | last post by:
Dear Python-list, I need to read a Unicode (utf-8) file that contains text like: I get my input and then process it with something like: When Python encounters the "\f" substring in an input...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.