file input problems (should be an easy question)

P

psy_berpunk

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

BobR

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 char> Image(
std::istreambuf_iterator<char>( PicIn.rdbuf() ),
std::istreambuf_iterator<char>() );
}// 1
// or:
{// 2
std::vector<unsigned char> Image;
std::copy(
std::istreambuf_iterator<char>( PicIn.rdbuf() ),
std::istreambuf_iterator<char>(),
std::back_inserter( Image ) );
}// 2
// or:
{// 3
std::vector<unsigned char> Image;
while( PicIn.peek() != EOF ){
Image.push_back( PicIn.get() );
}
}// 3
// or:
{// 4 int
std::vector<int> Image;
while( PicIn.peek() != EOF ){
Image.push_back( int( PicIn.get() ) ); // int() for illustration.
}
}// 4
 
J

Jack Klein

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
 
G

Guest

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.

To the OP:
After opening in binary mode you should use read() and write(), not the
 
J

James Kanze

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

James Kanze

Rule number 1: Read the full post before replying.
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.
 
R

red floyd

James said:
On Sep 24, 1:16 am, (e-mail address removed) wrote:

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++.
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top