/* Kenneth L Moore CIT 145 22/01/02 07:31 file strio.c String input and output */ #include #include #define WORKFILE "Workfile.work" /* prototype */ void stro( char * str, int strmax, char *name ); /* macro to invoke string display with length and name */ #define SHOWSTR( s ) stro( (s), sizeof( (s) ), #s ) int main ( void ) { long i; int count; char joe[11], moe[21], line[81]; FILE work; /* first, some regular scanf string input, printf output */ printf( "\nEnter a series of blank-delimited strings... up to 3\n" ); count = 0; while( scanf( "%10s", joe ) == 1 && (count < 3)) /* take in up to 10 chars at a time */ { SHOWSTR( joe ); count++; } fflush( stdin ); /* now, some scanf's with no stored null (%10c) */ count = 0; printf( "\nEnter a series of characters to be stored in string... up to 3\n" ); strcpy( joe, "**********" ); /* prime joe so you can see overlay */ while( scanf( "%5c", joe ) == 1 && (count < 3)) /* take in up to 5 chars at a time */ { SHOWSTR( joe ); count++; } fflush( stdin ); /* now some comma/newline-delimited strings, 20 chars max */ count = 0; printf( "\nEnter a series of strings delimited by commas... up to 3\n" ); while( scanf( "%20[^,\n]", moe ) >= 1 &&(count <3)) { SHOWSTR( moe ); count++; scanf( "%*[,\n]" ); } fflush( stdin ); /* some whole-line input */ printf( "\nEnter a line of input, no more than 80 chars (please?)\n" ); gets( line ); /* careful, no bound on length that can be entered! */ SHOWSTR( line ); printf( "\nEnter a line of input...\n" ); fgets( line, sizeof( line ), stdin ); SHOWSTR( line ); printf( "\nEnter line(s) of input...\n" ); while( fgets( moe, sizeof( moe ), stdin ) != NULL ) /* 20 chars max */ { SHOWSTR( moe ); } return( 0 ); } /* routine to display string contents */ void stro( char * str, int strmax, char *name ) { int i; printf( "sizeof(%s)=%d, strlen(%s)=%d\n", name, strmax, name, strlen( str ) ); printf( "contents " ); for( i = 0; i < strmax; i++ ) { printf( ( ( str[i] < ' ' ) ? "" : "<%c>" ), str[i] ); } printf( "\nPrints as <%s>\n\n", str ); }