{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C Programming Tutorial (K&R version 4) _Part4

C Programming Tutorial (K&R version 4) _Part4 -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
feof() This function returns a true or false result. It tests whether or not the end of a file has been reached and if it has it returns `true' (which has any value except zero); otherwise the function returns `false' (which has the value zero). The form of a statement using this function is: FILE *fp; int outcome; outcome = feof(fp); Most often feof() will be used inside loops or conditional statements. For example: consider a loop which reads characters from an open file, pointed to by fp . A call to feof() is required in order to check for the end of the file. while (!feof(fp)) { ch = getc(fp); } Translated into pidgin English, this code reads: `while NOT end of file, ch equals get character from file'. In better(?) English the loop continues to fetch characters as long as the end of the file has not been reached. Notice the logical NOT operator ! which stands before feof() . Node:Printer Output, Next: Example 36 , Previous: feof , Up: Files and Devices Printer Output Any serious application program will have to be in full control of the output of a program. For instance, it may need to redirect output to the printer so that data can be made into hard copies. To do this, one of three things must be undertaken: stdout must be redirected so that it sends data to the printer device. A new "standard file" must be used (not all C compilers use this method.) A new file must be opened in order to write to the printer device
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The first method is not generally satisfactory for applications programs, because the standard files stdin and stdout can only easily be redirected from the operating system command line interpreter (when a program is run by typing its name). Examples of this are: type file > PRN which send a text file to the printer device. The second method is reserved for only a few implementations of C in which another `standard file' is opened by the local operating system and is available for sending data to the printer stream. This file might be called "stdprn" or "standard printer file" and data could be written to the printer by switching writing to the file like this: fprintf (stdprn,"string %d...", integer); The final method of writing to the printer is to open a file to the printer, personally. To do this, a program has to give the "filename" of the printer device. This could be something like "PRT:" or "PRN" or "LPRT" or whatever. The filename (actually called a pseudo device name) is used to open a file in precisely the same way as any other file is opened: by using a call to fopen() . fopen() then returns a pointer to file (which is effectively "stdprn") and this is used to write data to a computer's printer driver. The program code to do this should look something like the following: FILE *stdprn; if ((stdprn = fopen("PRT:","w")) == NULL) { printf ("Printer busy or disconnected\n"); error_handler; } Node:Example 36, Next: Output 36 , Previous: Printer Output , Up: Files and Devices Example Here is an example program which reads a source file (for a program, written in C, Pascal or whatever...) and lists it, along with its line numbers. This kind of program is useful for debugging programs. The program provides the user with the option of
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}