Text mode or binary mode ? 
Author Message
 Text mode or binary mode ?

Hello world,

I'm writing a text compressor; it takes as input a text file and applies the
compression algorithm to the Ascii code (in binary format) of each character
read.
I need some advices to go on. More exactly:
- which is the actual difference between reading a file in text mode and in
binary mode? I know binary mode makes a one-to-one correspondence with the
input, but is there also a difference in the way read data are stored?
- which function should I use? getc(), fgets(), fread() ???
- once read a char in some way, how can I get its bits? Must I use &, | etc.
?
- once got a bit how can I use it? I mean, should I treat it as an int, a
char.. ?

Thank you very much,
Ale



Sat, 31 May 2003 00:09:16 GMT  
 Text mode or binary mode ?

Quote:

> I'm writing a text compressor; it takes as input a text file and applies the
> compression algorithm to the Ascii code (in binary format) of each character
> read.
> I need some advices to go on. More exactly:
> - which is the actual difference between reading a file in text mode and in
> binary mode? I know binary mode makes a one-to-one correspondence with the
> input, but is there also a difference in the way read data are stored?

The difference is system-specific.  On many systems, there is no
difference at all.  On other systems, there is some sort of
translation that takes place between internal and external
representations of text.  For instance, under MS-DOS, text files
on disk are represented using the two characters "\r\n" (13 and
10, respectively, in ASCII) to mark the end of a line, whereas
internally end-of-line is always indicated by a single "\n"
newline character.  When a file is read in binary mode, no such
translations occur.

Quote:
> - which function should I use? getc(), fgets(), fread() ???

Whichever is most convenient for what you're going to do with the
file data.  All file-reading functions are equivalent to some
number of calls to the getc() function (possibly followed by
ungetc() in the case of fscanf(), IIRC), so just use the one that
is easiest.

Quote:
> - once read a char in some way, how can I get its bits? Must I
> use &, | etc.  ?

That's the way to do it, yes.  For instance, to test whether the
bit with value 2**x is set in y, you could write `if (y & (1u <<
x))'.

(Bit manipulation should really be done on unsigned quantities,
by the way; the results are more strictly defined.)

Quote:
> - once got a bit how can I use it? I mean, should I treat it as an int, a
> char.. ?

Any integer type[1] is sufficient to hold a single bit, so you
may use any type you wish.

[1] Except a signed integer single-bit bit-field.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield



Sat, 31 May 2003 00:50:50 GMT  
 Text mode or binary mode ?


Quote:

> > I'm writing a text compressor...

Thanks for your suggestions, Ben.
Do you think I should read files to compress in binary mode instead text
mode? Does one get any advantage?
And about the instruction  if(y & (1u << x)) , I saw that sometimes y has
not the same size of the 'mask'. Is it correct? In your instruction, has y
to be a 16 bit variable ? What happens if it isn't ?

Alessandro



Tue, 03 Jun 2003 19:02:03 GMT  
 Text mode or binary mode ?


Quote:




>> > I'm writing a text compressor...

>Thanks for your suggestions, Ben.
>Do you think I should read files to compress in binary mode instead text
>mode? Does one get any advantage?

I think it is best to read the files in binary mode. That way you can
use your compression program also for non-text files.

Quote:
>And about the instruction  if(y & (1u << x)) , I saw that sometimes y has
>not the same size of the 'mask'. Is it correct? In your instruction, has y
>to be a 16 bit variable ? What happens if it isn't ?

In the expression 'y & (1u << x)', no specific sizes of x and y are
assumed. As long as y is of an unsigned integral type (unsigned char,
unsigned short, unsigned int, unsigned long, etc.) and x is positive,
the result will always be well defined.

If the operands og the operator & do not have the same size, one or
both operands will be promoted to a larger type, so that both operands
get the same type. In this promotion the original value will be
preserved.

Quote:

>Alessandro

Bart v Ingen Schenau
--
FAQ for clc: http://www.eskimo.com/~scs/C-faq/top.html


Wed, 04 Jun 2003 02:48:17 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. reading text files in binary mode

2. Binary and Text Modes and Stdin/Stdout

3. Is stdprn opened in binary or text mode?

4. readable binary in text mode

5. readable binary in text mode

6. ofstream binary mode not binary?

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

8. Works in Debug mode but not in Release mode

9. Video Mode 13h amd Mode X in C

10. ANNOUNCE: CC Mode 5.14, Emacs mode for C, C++, Java, ObjC, IDL

11. Protected Mode --> Real Mode Calls **HELP**

12. real mode interupts in protected mode

 

 
Powered by phpBB® Forum Software