1 // do some non-destructive read tests relating text files
5 static void readchars( FILE *fd ) {
7 while ( (c = fgetc(fd)) != EOF ) {
20 static void readlines( FILE *fd ) {
22 while ( fgets(line, 255, fd) != NULL ) {
23 int len = strlen(line);
24 printf("line = %s", line);
26 printf(" char[n - 1] = %d char[n] = %d\n",
27 line[len-2], line[len-1]);
28 } else if ( len >= 1 ) {
29 printf(" char[n] = %d\n",line[len-1]);
31 printf("empty string\n");
37 int main( int argc, char **argv ) {
40 printf("usage: %s file\n", argv[0]);
45 strcpy( file, argv[1] );
48 // open a file in (default) text mode
49 printf("TEXT MODE (DEFAULT) by character\n\n");
50 fd = fopen( file, "r" );
55 printf("Cannot open %s\n", file);
59 // open a file in (explicit) text mode
60 printf("TEXT MODE (EXPLICIT) by character\n\n");
61 fd = fopen( file, "rt" );
66 printf("Cannot open %s\n", file);
70 // open a file in (explicit) binary mode
71 printf("BINARY MODE (EXPLICIT) by character\n\n");
72 fd = fopen( file, "rb" );
77 printf("Cannot open %s\n", file);
81 // open a file in (default) text mode
82 printf("TEXT MODE (DEFAULT) by line\n\n");
83 fd = fopen( file, "r" );
88 printf("Cannot open %s\n", file);
92 // open a file in (explicit) text mode
93 printf("TEXT MODE (EXPLICIT) by line\n\n");
94 fd = fopen( file, "rt" );
99 printf("Cannot open %s\n", file);
103 // open a file in (explicit) binary mode
104 printf("BINARY MODE (EXPLICIT) by line\n\n");
105 fd = fopen( file, "rb" );
110 printf("Cannot open %s\n", file);