Practical C Programming by Oualline Steve

Practical C Programming by Oualline Steve

Author:Oualline, Steve [Steve Oualline]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C
ISBN: 9781449313043
Publisher: O'Reilly Media, Inc.
Published: 2011-07-12T16:00:00+00:00


Back in the dark ages BC (Before Computers), a magical device called a Teletype Model 33 existed. This amazing machine contained a shift register made out of a motor, with a rotor, and a keyboard ROM consisting solely of levers and springs. It contained a keyboard, a printer, and a paper tape reader/punch. It could transmit messages over the phones using a modem at the rate of 10 characters a second.

The Teletype had a problem. It took two-tenths of a second to move the printhead from the right side to the left. Two-tenths of a second is two character times. If a second character came while the printhead was in the middle of a return, that character was lost.

The Teletype people solved this problem by making end-of-line two characters: <RETURN> to position the printhead at the left margin and <LINE FEED> to move the paper up one line.

When the early computers came out, some designers realized that using two characters for end-of-line wasted storage (at this time, storage was very expensive). Some picked <LINE FEED> for their end-of-line, some <RETURN>. Some of the diehards stayed with the two-character sequence.

UNIX uses <LINE FEED> for end-of-line. The newline character, \n, is code 0x0A (LF or <LINE FEED>). MS-DOS/Windows uses the two characters: <RETURN><LINE FEED>. Apple uses <RETURN>.

MS-DOS/Windows compiler designers had a problem. What do we do about the old C programs that thought that newline was just <LINE FEED>? The solution was to add code to the I/O library that stripped out the <RETURN> characters from ASCII input files and changed <LINE FEED> to <LINE FEED> <RETURN> on output.

In MS-DOS/Windows, it makes a difference whether or not a file is opened as ASCII or binary. The flag b is used to indicate a binary file:

/* open ASCII file for reading */ ascii_file = fopen("name", "r"); /* open binary file for reading */ binary_file = fopen("name", "rb");

If you open a file that contains text as a binary file under MS-DOS/Windows, you have to handle the carriage returns in your program. If you open it as ASCII, the carriage returns are automatically removed by the read routines.

Question 14-2: The routine fputc can be used to write out a single byte of a binary file. Example 14-4 writes out numbers to 127 to a file called test.out. It works just fine on UNIX, creating a 128-byte-long file; however, on MS-DOS/Windows, the file contains 129 bytes. Why?

Example 14-4. wbin/wbin.c

[File: wbin/wbin.c] #include <stdio.h> #include <stdlib.h> #ifndef __MSDOS__ #include <unistd.h> #endif __MSDOS__ int main() { int cur_char; /* current character to write */ FILE *out_file; /* output file */ out_file = fopen("test.out", "w"); if (out_file == NULL) { fprintf(stderr,"Cannot open output file\n"); exit (8); } for (cur_char = 0; cur_char < 128; ++cur_char) { fputc(cur_char, out_file); } fclose(out_file); return (0); }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.