PAGEFLOP
FORWARD
P32
C Programming|C Structures|6-9.1|18,60
A structure is a collection of one or more (usually
two or more) variables, usually with differing data types
grouped under the same name. In the following example,
there are 4 variables that are grouped together into
the new structure type called postage and the variable
stamp_cost of this type is defined.
struct postage {
int class; /* mail class */
float weight; /* weight of object */
int region; /* region of delivery */
char object[10]; /* object name */
} stamp_cost;
postage is a name of the type of structure defined
above; we'll refer to this as a structure tag.
stamp_cost is a structure variable of type postage.
P33
C Programming|C Structures|6-9.2|12,50
The following uses a structure tag to declare
the variable "mail_cost":
struct postage mail_cost;
Refer to structure entries by specifying the
structure name followed by "." and the entry.
For example:
mail_cost.class = 2;
strcpy(mail_cost.object, "parcel");
P34
C Programming|C Structures|6-9.3|9,54
If an object is a pointer to a structure, refer
to entries by replacing . with -> :
delivery(mail_cost)
struct postage *mail_cost;
{
:
mail_cost->region = 7;
}
P35
C Programming|C Structures|6-9.4|7,50
The following example shows the usage of a
typical C structure.
Note, that "player" is a pointer variable,
therefore all the references to the elements of
those variables are done with the "->" symbol.
P36
C Programming|C Structures|6-9.5|13,36
struct person {
char name[30];
int age;
char sex;
};
tournament (player)
struct person *player;
{
strcpy(player->name, "Spencer");
player->age = 27;
player->sex = 'M';
}
P37
C Programming|File I/O|6-10.1|14,54
I/O features are not built into the C language.
All I/O is performed by function calls.
Buffered I/O is performed by using functions in
the C library, (Section 3 in the UNIX manual).
Many macros and constant definitions are defined in
the standard I/O file, stdio.h.
Mathematical constants normally reside in the math.h
file.
P38
C Programming|File I/O|6-10.2|17,58
The next example shows how text files may be
copied in C. The source file is "file_in" and the
destination file is "file_out", whose names are declared
as character string pointers. There are also two file
pointers "fp_in" and "fp_out" respectively.
Note that the function fopen() is used for
opening the files. The source file is opened for read -
"r", and the destination file is opened for write - "w",
which effectively creates a new file or erases the old one.
The statement that does actual copying is:
while ((c = getc(fp_in))!=EOF)
putc(c, fp_out);
P39
C Programming|File I/O|6-10.3|17,49
#include <stdio.h>
main()
{
int c;
char *file1 = "file_in", *file2 = "file_out";
FILE *fopen(), *fp_in, *fp_out;
/* OPEN INPUT FILE, CREATE OUTPUT FILE */
if ((fp_in = fopen(file1, "r")) != NULL) {
if ((fp_out =fopen(file2, "w"))
!= NULL) {
/* COPY FILES */
while ((c = getc(fp_in)) != EOF)
putc(c, fp_out);
fclose(fp_out);
} fclose(fp_in);
}
}
P40
C Programming|UNIX System Interface|6-11.1|16,56
System calls (see Section 2 in the UNIX manual)
provide direct, unbuffered access to the UNIX facilities.
System calls are used by functions performing
buffered I/O.
System calls refer to data by size rather than by
type.
System calls provide direct file access, file
opening, closing, reading, writing, creating, deleting,
process control, asynchronous execution, system
parameters, information and many others.
P41
C Programming|UNIX System Interface|6-11.2|10,55
In the following example we will again be
copying files. However, this time we will be using
the system calls read() and write() to perform
these functions.
These functions have 3 arguments: the file name,
the address of the buffer, and the number of bytes to
be read or written. Each function will return the
number of bytes transferred.
P42
C Programming|UNIX System Interface|6-11.3|14,55
A zero (0) return on read signifies end of file
(EOF). The requested data transfer rate is 512 bytes,
i.e., BUFSIZE.
Note that the destination file is created explicitly
with the system call creat() which has two arguments:
the file name and the file protection. Furthermore, the
system calls open() and close() are binary analogs of
fopen() and fclose().
System calls return -1 on a failure; on success
they return some reasonable integer.
P43
C Programming|UNIX System Interface|6-11.4|17,55
/* copy input file to output file */
#define BUFSIZE 512
main()
{ int bytes;
char buf[BUFSIZE];
int f_d_in, f_d_out;
/* open input for read, output for write */
if ((f_d_in = open("data_in", 0)) != 1) {
if ((f_d_out = creat("data_in", 0644)) != -1) {
while ((bytes=read(f_d_in, buf,BUFSIZE)) > 0)
write(f_d_out, buf, bytes);
close(f_d_out);
}
close(f_d_in);
}
}
P44
C Programming|The lint Utility|6-12.1|7,54
The lint utility is a program that examines
"C" source programs to detect bugs and obscurities.
The basic function of lint is to detect the
inconsistencies in the source programs which the
compiler would not detect.
P45
C Programming|The lint Utility|6-12.2|9,58
Some of lint's functions include:
* enforcing type rules,
* complaining about unused variables and functions,
* complaining about unreachable portions of the program,
* complaining about questionable casts,
* complaining about nonportable character use,
* complaining about strange C constructions, and
* complaining about pointer alignment.