1 2 3 4 5 6 7 8 9 10
The ReadLine and FetchLine functions, below, are equivalent, but their parameter declarations are appropriate for their intended use
#include <stdio.h> int ReadLine ( char str[], int n ) { int ch; int i = 0;
while ( (ch = getchar()) != '\n' ) { if ( i < n ) { str[i] = ch; i++; } } str[i] = '\0'; return (i);
int FetchLine (char* str, int n) { int ch; int i = 0;
while ( (ch = getchar()) != '\n' ) { if ( i < n ) { *(str+i) = ch; i++; } } *(str+i) = '\0'; return (i); } The functions are used in the following way:
January22, 1999
Page 7
copyright UoA