1 // do some non-destructive read tests relating text files
6 static void readchars( FILE *fd ) {
8 while ( (c = fgetc(fd)) != EOF ) {
21 static void readlines( FILE *fd ) {
23 while ( fgets(line, 255, fd) != NULL ) {
24 int len = strlen(line);
25 printf("line = %s", line);
27 printf(" char[n - 1] = %d char[n] = %d\n",
28 line[len-2], line[len-1]);
29 } else if ( len >= 1 ) {
30 printf(" char[n] = %d\n",line[len-1]);
32 printf("empty string\n");
38 int main( int argc, char **argv ) {
41 printf("usage: %s file\n", argv[0]);
46 strcpy( file, argv[1] );
49 // open a file in (default) text mode
50 printf("TEXT MODE (DEFAULT) by character\n\n");
51 fd = fopen( file, "r" );
56 printf("Cannot open %s\n", file);
60 // open a file in (explicit) text mode
61 printf("TEXT MODE (EXPLICIT) by character\n\n");
62 fd = fopen( file, "rt" );
67 printf("Cannot open %s\n", file);
71 // open a file in (explicit) binary mode
72 printf("BINARY MODE (EXPLICIT) by character\n\n");
73 fd = fopen( file, "rb" );
78 printf("Cannot open %s\n", file);
82 // open a file in (default) text mode
83 printf("TEXT MODE (DEFAULT) by line\n\n");
84 fd = fopen( file, "r" );
89 printf("Cannot open %s\n", file);
93 // open a file in (explicit) text mode
94 printf("TEXT MODE (EXPLICIT) by line\n\n");
95 fd = fopen( file, "rt" );
100 printf("Cannot open %s\n", file);
104 // open a file in (explicit) binary mode
105 printf("BINARY MODE (EXPLICIT) by line\n\n");
106 fd = fopen( file, "rb" );
111 printf("Cannot open %s\n", file);