| 1 |
// do some non-destructive read tests relating text files |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
static void readchars( FILE *fd ) { |
| 7 |
int c; |
| 8 |
while ( (c = fgetc(fd)) != EOF ) { |
| 9 |
printf("c = %d", c); |
| 10 |
if ( c == 10 ) { |
| 11 |
printf(" LF \\n"); |
| 12 |
} |
| 13 |
if ( c == 13 ) { |
| 14 |
printf(" CR \\r"); |
| 15 |
} |
| 16 |
printf("\n"); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
static void readlines( FILE *fd ) { |
| 22 |
char line[256]; |
| 23 |
while ( fgets(line, 255, fd) != NULL ) { |
| 24 |
int len = strlen(line); |
| 25 |
printf("line = %s", line); |
| 26 |
if ( len >= 2 ) { |
| 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]); |
| 31 |
} else { |
| 32 |
printf("empty string\n"); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
int main( int argc, char **argv ) { |
| 39 |
// check usage |
| 40 |
if ( argc != 2 ) { |
| 41 |
printf("usage: %s file\n", argv[0]); |
| 42 |
return -1; |
| 43 |
} |
| 44 |
|
| 45 |
char file[256]; |
| 46 |
strcpy( file, argv[1] ); |
| 47 |
FILE *fd; |
| 48 |
|
| 49 |
// open a file in (default) text mode |
| 50 |
printf("TEXT MODE (DEFAULT) by character\n\n"); |
| 51 |
fd = fopen( file, "r" ); |
| 52 |
if ( fd != NULL ) { |
| 53 |
readchars( fd ); |
| 54 |
fclose(fd); |
| 55 |
} else { |
| 56 |
printf("Cannot open %s\n", file); |
| 57 |
} |
| 58 |
printf("\n"); |
| 59 |
|
| 60 |
// open a file in (explicit) text mode |
| 61 |
printf("TEXT MODE (EXPLICIT) by character\n\n"); |
| 62 |
fd = fopen( file, "rt" ); |
| 63 |
if ( fd != NULL ) { |
| 64 |
readchars( fd ); |
| 65 |
fclose(fd); |
| 66 |
} else { |
| 67 |
printf("Cannot open %s\n", file); |
| 68 |
} |
| 69 |
printf("\n"); |
| 70 |
|
| 71 |
// open a file in (explicit) binary mode |
| 72 |
printf("BINARY MODE (EXPLICIT) by character\n\n"); |
| 73 |
fd = fopen( file, "rb" ); |
| 74 |
if ( fd != NULL ) { |
| 75 |
readchars( fd ); |
| 76 |
fclose(fd); |
| 77 |
} else { |
| 78 |
printf("Cannot open %s\n", file); |
| 79 |
} |
| 80 |
printf("\n"); |
| 81 |
|
| 82 |
// open a file in (default) text mode |
| 83 |
printf("TEXT MODE (DEFAULT) by line\n\n"); |
| 84 |
fd = fopen( file, "r" ); |
| 85 |
if ( fd != NULL ) { |
| 86 |
readlines( fd ); |
| 87 |
fclose(fd); |
| 88 |
} else { |
| 89 |
printf("Cannot open %s\n", file); |
| 90 |
} |
| 91 |
printf("\n"); |
| 92 |
|
| 93 |
// open a file in (explicit) text mode |
| 94 |
printf("TEXT MODE (EXPLICIT) by line\n\n"); |
| 95 |
fd = fopen( file, "rt" ); |
| 96 |
if ( fd != NULL ) { |
| 97 |
readlines( fd ); |
| 98 |
fclose(fd); |
| 99 |
} else { |
| 100 |
printf("Cannot open %s\n", file); |
| 101 |
} |
| 102 |
printf("\n"); |
| 103 |
|
| 104 |
// open a file in (explicit) binary mode |
| 105 |
printf("BINARY MODE (EXPLICIT) by line\n\n"); |
| 106 |
fd = fopen( file, "rb" ); |
| 107 |
if ( fd != NULL ) { |
| 108 |
readlines( fd ); |
| 109 |
fclose(fd); |
| 110 |
} else { |
| 111 |
printf("Cannot open %s\n", file); |
| 112 |
} |
| 113 |
printf("\n"); |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |