Add Book to My BookshelfPurchase This Book Online

Chapter 4 - The Standard I/O Library

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Line-Based Input and Output
The Standard I/O Library also provides functions that you can use to process files a line at a time, where a line is defined as some sequence of bytes terminated by a newline character:
    #include <stdio.h>
    char *gets(char *s);
    char *fgets(char *s, int n, FILE *stream);
    int puts(const char *s);
    int fputs(const char *s, FILE *stream);
The gets function reads characters from stdin and places them into s until either a newline character is read or end-of-file is encountered. The fgets function reads characters from stream and places them into s until either a newline character is encountered, n-1 characters have been read, or end-of-file is encountered. Both functions terminate s with a null character and return s, or return the constant NULL if end-of-file is encountered before any characters have been read. For historical reasons, gets discards the newline character, while fgets copies it into s.
 NoteThere is a significant problem with gets: it has no way of knowing the size of the array pointed to by its argument, s. It will happily continue reading characters and copying them to memory, even after s is filled, until it encounters a newline character or end-of-file. This has the unfortunate side effect of destroying the contents of whatever variables follow s in memory, resulting in unexpected program behavior. This “feature” of gets was used with great success by the 1988 Internet worm to gain unauthorized access to systems. Because of this problem, the gets function should be considered “evil” and you should avoid using it at all costs.
The puts function writes the string pointed to by s, followed by a newline character, to the standard output. The fputs function writes the string pointed to by s to stream but does not append a newline character. On success, both functions return the number of characters written. If an error occurs, they return the constant EOF.
Example 4-2 shows another version of our file-appending program; this one uses fgets and fputs to process the file a line at a time.
Example 4-2:  append-line
#include <stdio.h>
int
main(int argc, char **argv)
{
    FILE *in, *out;
    char line[BUFSIZ];
    if (argc != 3) {
        fprintf(stderr, "Usage: append-line file1 file2\n");
        exit(1);
    }
    /*
     * Open the first file for reading.
     */
    if ((in = fopen(argv[1], "r")) == NULL) {
        perror(argv[1]);
        exit(1);
    }
    /*
     * Open the second file for writing.
     */
    if ((out = fopen(argv[2], "a")) == NULL) {
        perror(argv[2]);
        exit(1);
    }
    /*
     * Copy data from the first file to the second, one line
     * at a time.
     */
    while (fgets(line, sizeof(line), in) != NULL)
        fputs(line, out);
    fclose(out);
    fclose(in);
    exit(0);
}
    % cat a
    file a line one
    file a line two
    file a line three
    % cat b
    file b line one
    file b line two
    file b line three
    % append-line a b
    % cat b
    file b line one
    file b line two
    file b line three
    file a line one
    file a line two
    file a line three

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback