473,414 Members | 1,684 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,414 software developers and data experts.

file input problems (should be an easy question)

hey, i'm trying to write a simple program to read gif87a non-
interlaced format with a single image-descriptor ---

I am using djgpp on windows xp.

Sounds simple enough, unfortunatly data in the format is arranged
primarily in single-byte unsigned integers.

So i've been reading them in a chars and casting them as unsigned
chars into an int.

No problem there, except that:

With cin, values of 12 and 13 are skipped.
With cin, in binary mode everything reads as 0;

with fgetc, fread, and fscanf values of 13 are skipped.

as u can imagine missing data really messes things up.

How can I read this file one byte at a time without having the
functions I'm using ignore important data?

Sep 23 '07 #1
8 1508

<ps*********@hotmail.comwrote in message
news:11*********************@r29g2000hsg.googlegro ups.com...
hey, i'm trying to write a simple program to read gif87a non-
interlaced format with a single image-descriptor ---
I am using djgpp on windows xp.

Sounds simple enough, unfortunatly data in the format is arranged
primarily in single-byte unsigned integers.
So i've been reading them in a chars and casting them as unsigned
chars into an int.
No problem there, except that:
With cin, values of 12 and 13 are skipped.
With cin, in binary mode everything reads as 0;
with fgetc, fread, and fscanf values of 13 are skipped.
as u can imagine missing data really messes things up.

How can I read this file one byte at a time without having the
functions I'm using ignore important data?
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator // #include <stream_iterator.h>

std::ifstream PicIn( "MyPic.png", std::ios_base::in|std::ios_base::binary );
if( not PicIn.is_open() ){
std::cout<<"\n ifstream open FAILED"<<std::endl;
}

// pick one
{// 1
std::vector<unsigned charImage(
std::istreambuf_iterator<char>( PicIn.rdbuf() ),
std::istreambuf_iterator<char>() );
}// 1
// or:
{// 2
std::vector<unsigned charImage;
std::copy(
std::istreambuf_iterator<char>( PicIn.rdbuf() ),
std::istreambuf_iterator<char>(),
std::back_inserter( Image ) );
}// 2
// or:
{// 3
std::vector<unsigned charImage;
while( PicIn.peek() != EOF ){
Image.push_back( PicIn.get() );
}
}// 3
// or:
{// 4 int
std::vector<intImage;
while( PicIn.peek() != EOF ){
Image.push_back( int( PicIn.get() ) ); // int() for illustration.
}
}// 4

--
Bob R
POVrookie
Sep 24 '07 #2
On Sun, 23 Sep 2007 16:16:59 -0700, ps*********@hotmail.com wrote in
comp.lang.c++:
hey, i'm trying to write a simple program to read gif87a non-
interlaced format with a single image-descriptor ---

I am using djgpp on windows xp.

Sounds simple enough, unfortunatly data in the format is arranged
primarily in single-byte unsigned integers.

So i've been reading them in a chars and casting them as unsigned
chars into an int.

No problem there, except that:

With cin, values of 12 and 13 are skipped.
Rule number 1: Open binary files in binary mode.
Rule number 2: Don't ever forget rule number 1.
Rule number 3: Don't ever forget rule number 1.
Rule number 4: Don't ever forget rule number 1.

I could continue, but I hope you're begriming to see a pattern.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 24 '07 #3
On 2007-09-24 05:05, Jack Klein wrote:
On Sun, 23 Sep 2007 16:16:59 -0700, ps*********@hotmail.com wrote in
comp.lang.c++:
>hey, i'm trying to write a simple program to read gif87a non-
interlaced format with a single image-descriptor ---

I am using djgpp on windows xp.

Sounds simple enough, unfortunatly data in the format is arranged
primarily in single-byte unsigned integers.

So i've been reading them in a chars and casting them as unsigned
chars into an int.

No problem there, except that:

With cin, values of 12 and 13 are skipped.

Rule number 1: Open binary files in binary mode.
Rule number 2: Don't ever forget rule number 1.
Rule number 3: Don't ever forget rule number 1.
Rule number 4: Don't ever forget rule number 1.

I could continue, but I hope you're begriming to see a pattern.
Rule number 1: Read the full post before replying.
>With cin, in binary mode everything reads as 0;
To the OP:
After opening in binary mode you should use read() and write(), not the
>and << operators, since those are for formatted (text) IO.
--
Erik Wikström
Sep 24 '07 #4
On Sep 24, 1:16 am, psy_berp...@hotmail.com wrote:
hey, i'm trying to write a simple program to read gif87a non-
interlaced format with a single image-descriptor ---
I am using djgpp on windows xp.
Sounds simple enough, unfortunatly data in the format is arranged
primarily in single-byte unsigned integers.
So i've been reading them in a chars and casting them as unsigned
chars into an int.
No problem there, except that:
With cin, values of 12 and 13 are skipped.
And 26 is probably interpreted as EOF. Those are the Windows
conventions for text files. You can't read a binary file in
text mode.
With cin, in binary mode everything reads as 0;
There's no way to put std::cin in binary mode. You'll have to
explain what you mean. (But for starters, it's imposible to
read a binary file, such as you describe, on std::cin. Or stdin
in C.)
with fgetc, fread, and fscanf values of 13 are skipped.
And 26 should be interpreted as EOF. The rules are the same for
C and for C++. (In fact, the semantics of fstream are described
by reference to the semantics of FILE* in C.)
as u can imagine missing data really messes things up.
How can I read this file one byte at a time without having the
functions I'm using ignore important data?
You'll have to open it as a file, using the std::ios::binary
flag. There's no provision for reading binary from standard in.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 24 '07 #5
On Sep 24, 9:51 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-24 05:05, Jack Klein wrote:
On Sun, 23 Sep 2007 16:16:59 -0700, psy_berp...@hotmail.com wrote in
comp.lang.c++:
hey, i'm trying to write a simple program to read gif87a non-
interlaced format with a single image-descriptor ---
I am using djgpp on windows xp.
Sounds simple enough, unfortunatly data in the format is arranged
primarily in single-byte unsigned integers.
So i've been reading them in a chars and casting them as unsigned
chars into an int.
No problem there, except that:
With cin, values of 12 and 13 are skipped.
Rule number 1: Open binary files in binary mode.
Rule number 2: Don't ever forget rule number 1.
Rule number 3: Don't ever forget rule number 1.
Rule number 4: Don't ever forget rule number 1.
I could continue, but I hope you're begriming to see a pattern.
Rule number 1: Read the full post before replying.
With cin, in binary mode everything reads as 0;
To the OP:
After opening in binary mode you should use read() and
write(), not the >and << operators, since those are for
formatted (text) IO.
You both seem to be forgetting that std::cin can never be in
binary mode. The only place you can specify the mode is when
you open the file, and std::cin is already opened when you gain
control.

And of course, istream::get() works just fine for binary, too.
If his goal is to get an array of int's with the values, then:

int ch = file.get() ;
while ( ch != EOF ) {
dest.push_back( ch ) ;
ch = file.get() ;
}

works just fine, on a file opened in binary mode.

Given that the original poster didn't complain about values like
9, 10 or 32 (white space, in text) being missing, I rather doubt
that he was using >to read the values. The missing values he
did complain about correspond very closely to values that would
be removed when translating a in text mode under Windows. And
since he said he was reading from std::cin, we know that he was
reading a file opened in text mode.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 24 '07 #6
James Kanze wrote:
On Sep 24, 1:16 am, psy_berp...@hotmail.com wrote:
>How can I read this file one byte at a time without having the
functions I'm using ignore important data?

You'll have to open it as a file, using the std::ios::binary
flag. There's no provision for reading binary from standard in.
Is this restriction going to be maintained in C++0x? It seems as if it
would discourage writing filters in C++.
Sep 24 '07 #7
On Mon, 24 Sep 2007 07:51:30 GMT, Erik Wikström
<Er***********@telia.comwrote in comp.lang.c++:
On 2007-09-24 05:05, Jack Klein wrote:
On Sun, 23 Sep 2007 16:16:59 -0700, ps*********@hotmail.com wrote in
comp.lang.c++:
hey, i'm trying to write a simple program to read gif87a non-
interlaced format with a single image-descriptor ---

I am using djgpp on windows xp.

Sounds simple enough, unfortunatly data in the format is arranged
primarily in single-byte unsigned integers.

So i've been reading them in a chars and casting them as unsigned
chars into an int.

No problem there, except that:

With cin, values of 12 and 13 are skipped.
Rule number 1: Open binary files in binary mode.
Rule number 2: Don't ever forget rule number 1.
Rule number 3: Don't ever forget rule number 1.
Rule number 4: Don't ever forget rule number 1.

I could continue, but I hope you're begriming to see a pattern.

Rule number 1: Read the full post before replying.
With cin, in binary mode everything reads as 0;
Oops, I actually did. And then it apparently leaked out of my left
ear, certainly not the right.

Thanks for the correction.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 25 '07 #8
On Sep 24, 5:33 pm, red floyd <no.s...@here.dudewrote:
James Kanze wrote:
On Sep 24, 1:16 am, psy_berp...@hotmail.com wrote:
How can I read this file one byte at a time without having the
functions I'm using ignore important data?
You'll have to open it as a file, using the std::ios::binary
flag. There's no provision for reading binary from standard in.
Is this restriction going to be maintained in C++0x?
Probably, since changing the mode on an open file can't be
implemented in many systems.
It seems as if it would discourage writing filters in C++.
It is a pain, sometimes, but most filters deal with text, so
it's not too much of a problem.

Personally, I'd be in favor of it, with restrictions: a
conformant implementation is allowed to make it always fail, and
it must be called before the first I/O. But as far as I know,
no one has even written up a proposal, so it won't be in the
next version of the standard.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 25 '07 #9

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

Similar topics

13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
20
by: Johan | last post by:
How can I include one XML file into another XML file (on the client side, in Firefox)? I think XInclude is just what I need, but Firefox doesn't support it:...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
14
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.