From 763193237b2fd4e79eb7a16a0ca938ee081508d6 Mon Sep 17 00:00:00 2001 From: curt Date: Tue, 17 Apr 2001 13:35:38 +0000 Subject: [PATCH] Initial revision. --- tests/test-text.cxx | 115 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/test-text.cxx diff --git a/tests/test-text.cxx b/tests/test-text.cxx new file mode 100644 index 000000000..e01782ec9 --- /dev/null +++ b/tests/test-text.cxx @@ -0,0 +1,115 @@ +// do some non-destructive read tests relating text files + +#include + +static void readchars( FILE *fd ) { + char c; + while ( (c = fgetc(fd)) != EOF ) { + printf("c = %d", c); + if ( c == 10 ) { + printf(" LF \\n"); + } + if ( c == 13 ) { + printf(" CR \\r"); + } + printf("\n"); + } +} + + +static void readlines( FILE *fd ) { + char line[256]; + while ( fgets(line, 255, fd) != NULL ) { + int len = strlen(line); + printf("line = %s", line); + if ( len >= 2 ) { + printf(" char[n - 1] = %d char[n] = %d\n", + line[len-2], line[len-1]); + } else if ( len >= 1 ) { + printf(" char[n] = %d\n",line[len-1]); + } else { + printf("empty string\n"); + } + } +} + + +int main( int argc, char **argv ) { + // check usage + if ( argc != 2 ) { + printf("usage: %s file\n", argv[0]); + return -1; + } + + char file[256]; + strcpy( file, argv[1] ); + FILE *fd; + + // open a file in (default) text mode + printf("TEXT MODE (DEFAULT) by character\n\n"); + fd = fopen( file, "r" ); + if ( fd != NULL ) { + readchars( fd ); + fclose(fd); + } else { + printf("Cannot open %s\n", file); + } + printf("\n"); + + // open a file in (explicit) text mode + printf("TEXT MODE (EXPLICIT) by character\n\n"); + fd = fopen( file, "rt" ); + if ( fd != NULL ) { + readchars( fd ); + fclose(fd); + } else { + printf("Cannot open %s\n", file); + } + printf("\n"); + + // open a file in (explicit) binary mode + printf("BINARY MODE (EXPLICIT) by character\n\n"); + fd = fopen( file, "rb" ); + if ( fd != NULL ) { + readchars( fd ); + fclose(fd); + } else { + printf("Cannot open %s\n", file); + } + printf("\n"); + + // open a file in (default) text mode + printf("TEXT MODE (DEFAULT) by line\n\n"); + fd = fopen( file, "r" ); + if ( fd != NULL ) { + readlines( fd ); + fclose(fd); + } else { + printf("Cannot open %s\n", file); + } + printf("\n"); + + // open a file in (explicit) text mode + printf("TEXT MODE (EXPLICIT) by line\n\n"); + fd = fopen( file, "rt" ); + if ( fd != NULL ) { + readlines( fd ); + fclose(fd); + } else { + printf("Cannot open %s\n", file); + } + printf("\n"); + + // open a file in (explicit) binary mode + printf("BINARY MODE (EXPLICIT) by line\n\n"); + fd = fopen( file, "rb" ); + if ( fd != NULL ) { + readlines( fd ); + fclose(fd); + } else { + printf("Cannot open %s\n", file); + } + printf("\n"); + + return 0; +} -- 2.39.5