File: test-text.cxx

package info (click to toggle)
flightgear 1:2016.4.4+dfsg-3+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,512 kB
  • ctags: 71,727
  • sloc: cpp: 195,395; ansic: 177,152; sh: 13,661; perl: 4,475; python: 2,381; asm: 642; makefile: 398; java: 314; xml: 85
file content (116 lines) | stat: -rw-r--r-- 2,428 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// do some non-destructive read tests relating text files

#include <stdio.h>
#include <string.h>

static void readchars( FILE *fd ) {
    int 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;
}