[ Pobierz całość w formacie PDF ]
read characters into the input buffer until it either finds a newline character, or it reads the
maximum number of characters allowed minus one. It leaves one character for the end of string
NULL character. In addition, if it finds an EOF, it will return a value of NULL. In our example,
when the EOF is found, the pointer "c" will be assigned the value of NULL. NULL is defined
as zero in your "stdio.h" file.
When we find that "c" has been assigned the value of NULL, we can stop processing data, but
we must check before we print just like in the last program.
Last of course, we close the file.
10.11 How To Use A Variable Filename
Load and display the fileanyfile.cfor an example of reading from any file. This program
asks the user for the filename desired, reads in the filename and opens that file for reading. The
entire file is then read and displayed on the monitor. It should pose no problems to your
understanding so no additional comments will be made.
#include "stdio.h"
File Input/Output C Tutorial 10-5
main( )
{
FILE *fp1;
char oneword[100],filename[25];
char *c;
printf("enter filename -> ");
scanf("%s",filename); /* read the desired filename */
fp1 = fopen(filename,"r");
do {
c = fgets(oneword,100,fp1); /* get one line from the file */
if (c != NULL)
printf("%s",oneword); /* display it on the monitor */
} while (c != NULL); /* repeat until NULL */
fclose(fp1);
}
Compile and run this program. When it requests a filename, enter the name and extension of
any text file available, even one of the example C programs.
10.12 How Do We Print?
Load the last example file in this chapter, the one namedprintdat.cfor an example of how
to print. This program should not present any surprises to you so we will move very quickly
through it.
#include "/sys/stdio.h"
main( )
{
FILE *funny,*printer;
int c;
funny = fopen("TENLINES.TXT","r"); /* open input file */
printer = fopen("PRN","w"); /* open printer file */
do {
c = getc(funny); /* got one character from the file */
if (c != EOF) {
putchar(c); /* display it on the monitor */
putc(c,printer); /* print the character */
}
} while (c != EOF); /* repeat until EOF (end of file) */
fclose(funny);
fclose(printer);
}
Once again, we open TENLINES.TXT for reading and we open PRN for writing. Printing is
identical to writing data to a disk file except that we use a standard name for the filename. There
are no definite standards as far as the name or names to be used for the printer, but the 1616/OS
names are, "CENT:", "SA:", and "SB:". Check your documentation for your particular
implementation.
Some of the newest MS-DOS compilers use a predefined file pointer such as "stdprn" for the
print file. Once again, check your documentation.
The program is simply a loop in which a character is read, and if it is not the EOF, it is displayed
and printed. When the EOF is found, the input file and the printer output files are both closed.
You can now erase TENLINES.TXT from your disk. We will not be using it in any of the later
chapters.
10-6 C Tutorial File Input/Output
10.13 Programming Exercises
1. Write a program that will prompt for a filename for a read file, prompt for a filename for a
write file, and open both plus a file to the printer. Enter a loop that will read a character, and
output it to the file, the printer, and the monitor. Stop at EOF.
2. Prompt for a filename to read. Read the file a line at a time and display it on the monitor
with line numbers.
File Input/Output C Tutorial 10-7
11
Structures and Unions
11.1 What Is A Structure?
A structure is a user defined data type. You have the ability to define a new type of data
considerably more complex than the types we have been using. A structure is a combination
of several different previously defined data types, including other structures we have defined.
An easy to understand definition is, a structure is a grouping of related data in a way convenient
to the programmer or user of the program. The best way to understand a structure is to look at
an example, so if you will load and displaystruct1.c,we will do just that.
main( )
{
struct {
char initial; /* last name initial */
int age; /* childs age */
int grade; /* childs grade in school */
} boy,girl;
boy.initial = R ;
boy.age = 15;
boy.grade = 75;
girl.age = boy.age - 1; /* she is one year younger */
[ Pobierz całość w formacie PDF ]