Topic : Standart C Library for Unix
Author : James Rogers
Page : << Previous 4  Next >>
Go to page :


       /* are we currently in or out of a word */

    /* initialize the count and set the state to outside a word */
    state=OUT;
    characters = words = lines = 0;

    /* get one character at a time from standard in, until EOF */
    while ((c = getchar()) != EOF) {
        characters++;        /* increment the count of characters */
        switch(c) {
        case '\n' :
            lines++;         /* increment the count of lines */
            state = OUT;     /* new-line is white space, outside word */
            break;
        case ' ' :
            state = OUT;     /* space is white space, outside word */
            break;
        case '\t':
            state = OUT;     /* tab is white space, outsides word */
            break;
        default :            /* otherwise we are in a word */
            if (state == OUT) {
                             /* if state is still out and we are in a word */
                             /* then we are at the first letter of the word */
            state = IN;      /* set the state to in */
            words++;         /* increment the count of words */
            }
            break;
        }
    }
    /*  print the results with a formatted print statement */
    printf("%d %d %d\n", characters, words, lines);
}


  

To read a character from a stream,

    int   getchar (void);
    int   getc(FILE *stream);
    int  fgetc(FILE *stream);
    int ungetc(int c, FILE *stream);


void this is left blank.
FILE *stream is an already existing stream.
int c is a character to be pushed back into the stream.

These functions return an int with the value of the next character from the stream.  If there are no more characters then the end-of-file indicator is set for the stream and the function returns EOF.  If there was a read error then the error indicator is set for the stream and the function returns EOF.

getchar is used to read a single character from standard input.

    int c;
    while((c = getchar()) != EOF) {
         <while not the end of file read and process each character>
    }

getc is used to read a single character from a stream.

    int c;
    FILE *stream;
    if((stream = fopen ("filename", "r")) != (FILE *)0) {
       while((c = getc(stream)) != EOF) {
         <process each character>
       }
    } else {
        <do error handling>
    }


fgetc is used to read a single character from a stream.  getchar and getc are written in terms of fgetc: getchar() is the same as fgetc(STDIN) and getc(x) is the same as fgetc(x).

    int c;
    FILE *stream;
    if((stream = fopen ("filename", "r")) != (FILE *)0) {
       while((c = fgetc(stream)) != EOF) {
         <process each character>
       }
    } else {
        <do error handling>
    }


ungetc is used to push a character back into the stream when you have read one character too many.  This is a common problem for compilers and pattern scanners.  It is possible to push back more than one character but this is not recommended as it is not portable.  An ungetc should follow a read and only push back a single character.

    int c;
    FILE *stream;
    if((stream = fopen ("filename", "r")) != (FILE *)0) {
       while((c = fgetc(stream)) != EOF) {
         <process each character>
         if (some_condition) {
            ungetc(c,stream);
            break;
         }
       }
    } else {
        do error handling
    }


To write a character to a stream,

    int  putchar(int c);
    int  putc(int c, FILE *stream);
    int fputc(int c, FILE *stream);


FILE *stream is an already existing stream.
int c is the character to be written to the stream.

These functions return the character written upon success.  If a write error occurs the error indicator is set for the stream and the function returns an EOF.

putchar writes a character to standard out.  putchar(x) is the same as fputc(x, STDIN)
  
    putchar('x');

putc writes a character to the stream.  putc(x,y) is the same as fputc(x,y)

    int c;
    FILE *stream;
    c='x';
    if((stream = fopen ("filename", "w")) != (FILE *)0) {
        putc(c, stream);
    } else {
        <error handling>
    }


fputc writes a character to the stream.

    int c;
    FILE *stream;
    c='y';
    if((stream = fopen ("filename", "w")) != (FILE *)0) {
        fputc(c, stream);
    } else {
        <error handling>
    }


To read a string from a stream,

    char  *gets(char *s);
    char *fgets(char *s, int n, FILE *stream);


char *s the string that will hold the result.
int n the maximum number of characters to read.
FILE *stream is an already existing stream.

If the read is successful then the pointer to s is returned.  If EOF is encountered and no characters have been read into the string then the string remains unchanged and a null pointer is returned.  If a read error occurs then the string contents are possibly changed in an undefined manner and a null pointer is returned.

gets reads from

Page : << Previous 4  Next >>