]> git.mxchange.org Git - flightgear.git/commitdiff
Initial revision.
authorcurt <curt>
Tue, 17 Apr 2001 13:35:38 +0000 (13:35 +0000)
committercurt <curt>
Tue, 17 Apr 2001 13:35:38 +0000 (13:35 +0000)
tests/test-text.cxx [new file with mode: 0644]

diff --git a/tests/test-text.cxx b/tests/test-text.cxx
new file mode 100644 (file)
index 0000000..e01782e
--- /dev/null
@@ -0,0 +1,115 @@
+// do some non-destructive read tests relating text files
+
+#include <stdio.h>
+
+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;
+}