reading text files in binary mode 
Author Message
 reading text files in binary mode

Does opening a text file in binary mode give you a more accurate
"picture" of the file.  I am opening a file in "r" mode and there seems
to be some garbage characters at the end of the file that are screwing
up my algorithm big time.

If so, can someone point me in the right direction on how this is done?

Thanks,
evil

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 24 Apr 2002 03:00:00 GMT  
 reading text files in binary mode

Quote:
> / Does opening a text file in binary mode give you a more accurate
> / "picture" of the file.  I am opening a file in "r" mode and there
seems

> Depending on your system, text i/o can map the new line character
'\n' in
> your source into various other representations like <LF>, <CR>,
<CR><LF>,
> <US>, etc

> / If so, can someone point me in the right direction on how this is
done?

> You can try open mode "rb".

That's it?  I don't have to make any other changes in my code?

evil

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 25 Apr 2002 03:00:00 GMT  
 reading text files in binary mode

Quote:

> > / Does opening a text file in binary mode give you a more accurate
> > / "picture" of the file.  I am opening a file in "r" mode and there
[snip]
> > You can try open mode "rb".

> That's it?  I don't have to make any other changes in my code?

That's basically it. Read it byte by byte into unsigned chars and print
their hex/octal/decimal representations.

Gergo

--
Make no little plans; they have no magic to stir men's {*filter*}.
                -- Daniel Hudson Burnham

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Thu, 25 Apr 2002 03:00:00 GMT  
 reading text files in binary mode
Gergo Barany a crit dans le message ...

Quote:

>> > / Does opening a text file in binary mode give you a more accurate
>> > / "picture" of the file.  I am opening a file in "r" mode and there
>[snip]
>> > You can try open mode "rb".

>> That's it?  I don't have to make any other changes in my code?

>That's basically it. Read it byte by byte into unsigned chars and print
>their hex/octal/decimal representations.

Not an unsigned char but an int rather, because the value of EOF is by
definition a negative int and have to be tested after any read, and not to
be processed like a data.

--
HS
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
About gets() and fflush(stdin) :
- "Even Buffy cannot slay the resulting RhinoDaemons."
Martin Ambuhl clc.



Thu, 25 Apr 2002 03:00:00 GMT  
 reading text files in binary mode

Quote:

> Gergo Barany a crit dans le message ...

> >> > / Does opening a text file in binary mode give you a more accurate
> >> > / "picture" of the file.  I am opening a file in "r" mode and there
> >[snip]
> >> > You can try open mode "rb".

> >> That's it?  I don't have to make any other changes in my code?

> >That's basically it. Read it byte by byte into unsigned chars and print
> >their hex/octal/decimal representations.

> Not an unsigned char but an int rather, because the value of EOF is by
> definition a negative int and have to be tested after any read, and not to
> be processed like a data.

I was thinking of reading the unsigned chars and checking feof() after
each read. But your way is just as good (or even better).

Gergo

--
"Because he's a character who's looking for his own identity, [He-Man is]
an interesting role for an actor."
-- Dolph Lundgren, "actor"

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Thu, 25 Apr 2002 03:00:00 GMT  
 reading text files in binary mode

Quote:


> > Not an unsigned char but an int rather, because the value of EOF is by
> > definition a negative int and have to be tested after any read, and not to
> > be processed like a data.

> I was thinking of reading the unsigned chars and checking feof() after
> each read. But your way is just as good (or even better).

Much better; feof() doesn't get set until after you've read one
character beyond the end of the file, not as you read the
last character. This, btw, is in the FAQ.

Richard



Fri, 26 Apr 2002 03:00:00 GMT  
 reading text files in binary mode

Quote:



> > > Not an unsigned char but an int rather, because the value of EOF is by
> > > definition a negative int and have to be tested after any read, and not to
> > > be processed like a data.

> > I was thinking of reading the unsigned chars and checking feof() after
> > each read. But your way is just as good (or even better).

> Much better; feof() doesn't get set until after you've read one
> character beyond the end of the file, not as you read the
> last character. This, btw, is in the FAQ.

I am aware of that. This code:

unsigned char c;
c = getchar();
while(!feof(stdin))
{
  printf("%x ", c);
  c = getchar();

Quote:
}

is clumsy, but it works.

Gergo

--
The goys have proven the following theorem...
                -- Physicist John von Neumann, at the start of a classroom
                   lecture.

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Fri, 26 Apr 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Binary file read to New text file save grief

2. Text mode or binary mode ?

3. Reading files in binary mode?

4. How to read a bmp file in binary mode

5. reading a binary file back to binary

6. Print a text file in text mode to the printer

7. Binary and Text Modes and Stdin/Stdout

8. Reading in binary mode

9. Is stdprn opened in binary or text mode?

10. readable binary in text mode

11. readable binary in text mode

12. *help reading char in text mode at (X,Y)

 

 
Powered by phpBB® Forum Software