11
11
#include <sys/stat.h>
12
12
#include <unistd.h>
13
13
14
- int main ()
15
- {
16
- // Reading
17
-
14
+ // Reading
15
+ void test_reading () {
18
16
FILE * file = fopen ("somefile.binary" , "rb" );
19
17
assert (file );
20
18
@@ -38,28 +36,29 @@ int main()
38
36
free (buffer );
39
37
40
38
// Do it again, with a loop on feof
41
-
42
39
printf ("loop: " );
43
40
file = fopen ("somefile.binary" , "rb" );
44
41
assert (file );
45
42
while (!feof (file )) {
46
43
char c = fgetc (file );
47
44
if (c != EOF ) printf ("%d " , c );
48
45
}
49
- fclose (file);
46
+ fclose (file );
50
47
printf ("\n" );
48
+ }
51
49
52
- // Standard streams
53
-
50
+ // Standard streams
51
+ void test_stdstreams () {
54
52
char gets_buffer [1024 ];
55
- printf (" input:%s\n " , gets (gets_buffer));
53
+ printf ("input:%s\n" , fgets (gets_buffer , 1024 , stdin ));
56
54
fwrite ("texto\n" , 1 , 6 , stdout );
57
55
fwrite ("texte\n" , 1 , 6 , stderr );
58
56
putchar ('$' );
59
57
putc ('\n' , stdout );
58
+ }
60
59
61
- // Writing
62
-
60
+ // Writing
61
+ void test_writing () {
63
62
char data [5 ] = { 10 , 30 , 20 , 11 , 88 };
64
63
FILE * outf = fopen ("go.out" , "wb" );
65
64
fwrite (data , 1 , 5 , outf );
@@ -73,14 +72,16 @@ int main()
73
72
int num = fread (data2 , 1 , 10 , inf );
74
73
fclose (inf );
75
74
printf ("%d : %d,%d,%d,%d,%d\n" , num , data2 [0 ], data2 [1 ], data2 [2 ], data2 [3 ], data2 [4 ]);
75
+ }
76
76
77
+ void test_seeking () {
77
78
// Test reading a file that has not been cached
78
-
79
+
79
80
FILE * other = fopen ("test.file" , "r" );
80
81
assert (other );
81
82
82
83
char otherData [1000 ];
83
- num = fread (otherData, 1 , 9 , other);
84
+ int num = fread (otherData , 1 , 9 , other );
84
85
otherData [num ] = 0 ;
85
86
printf ("other=%s.\n" , otherData );
86
87
@@ -102,21 +103,24 @@ int main()
102
103
printf ("seeked=%s.\n" , otherData );
103
104
104
105
fclose (other );
106
+ }
105
107
106
- // fscanf
107
-
108
- outf = fopen (" fscan.f" , " w" );
108
+ // fscanf
109
+ void test_fscanf () {
110
+ FILE * outf = fopen ("fscan.f" , "w" );
109
111
fprintf (outf , "10 hello" );
110
112
fclose (outf );
111
113
112
114
int number ;
113
115
char text [100 ];
114
- inf = fopen (" fscan.f" , " r" );
115
- num = fscanf (inf, " %d %s" , &number, text);
116
+ FILE * inf = fopen ("fscan.f" , "r" );
117
+ int num = fscanf (inf , "%d %s" , & number , text );
116
118
fclose (inf );
117
119
printf ("fscanfed: %d - %s\n" , number , text );
120
+ }
118
121
119
- // temp files
122
+ // temp files
123
+ void test_tempfiles () {
120
124
const char * tname = "file_XXXXXX" ;
121
125
char tname1 [100 ];
122
126
char tname2 [100 ];
@@ -142,20 +146,28 @@ int main()
142
146
assert (strncmp ("/tmp/" , str , 5 ) == 0 );
143
147
}
144
148
149
+ char data [5 ] = { 10 , 30 , 20 , 11 , 88 };
145
150
FILE * n = fopen ("/dev/null" , "w" );
146
151
printf ("5 bytes to dev/null: %zu\n" , fwrite (data , 1 , 5 , n ));
147
152
fclose (n );
148
-
153
+
149
154
// Test file creation with O_TRUNC (regression test for #16784)
150
155
const char * file_name = "test.out" ;
151
156
int fd = open (file_name , O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR );
152
157
assert (fd >= 0 );
153
158
int status = write (fd , "blablabla\n" , 10 );
154
159
assert (status == 10 );
155
160
close (fd );
161
+ }
156
162
163
+ int main () {
164
+ test_reading ();
165
+ test_stdstreams ();
166
+ test_writing ();
167
+ test_seeking ();
168
+ test_fscanf ();
169
+ test_tempfiles ();
157
170
printf ("ok.\n" );
158
-
159
171
return 0 ;
160
172
}
161
173
0 commit comments