1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #define TESTFILE "conftest.tmp" |
---|
5 | int |
---|
6 | main (void) |
---|
7 | { |
---|
8 | FILE *fp; |
---|
9 | |
---|
10 | /* Create a file with some contents. */ |
---|
11 | fp = fopen (TESTFILE, "w"); |
---|
12 | if (fp == NULL) |
---|
13 | return 70; |
---|
14 | if (fwrite ("foogarsh", 1, 8, fp) < 8) |
---|
15 | { fclose (fp); return 71; } |
---|
16 | if (fclose (fp)) |
---|
17 | return 72; |
---|
18 | |
---|
19 | /* The file's contents is now "foogarsh". */ |
---|
20 | |
---|
21 | /* Try writing after reading to EOF. */ |
---|
22 | fp = fopen (TESTFILE, "r+"); |
---|
23 | if (fp == NULL) |
---|
24 | return 73; |
---|
25 | if (fseek (fp, -1, SEEK_END)) |
---|
26 | { fclose (fp); return 74; } |
---|
27 | if (!(getc (fp) == 'h')) |
---|
28 | { fclose (fp); return 1; } |
---|
29 | if (!(getc (fp) == EOF)) |
---|
30 | { fclose (fp); return 2; } |
---|
31 | if (!(ftell (fp) == 8)) |
---|
32 | { fclose (fp); return 3; } |
---|
33 | if (!(ftell (fp) == 8)) |
---|
34 | { fclose (fp); return 4; } |
---|
35 | if (!(putc ('!', fp) == '!')) |
---|
36 | { fclose (fp); return 5; } |
---|
37 | if (!(ftell (fp) == 9)) |
---|
38 | { fclose (fp); return 6; } |
---|
39 | if (!(fclose (fp) == 0)) |
---|
40 | return 7; |
---|
41 | fp = fopen (TESTFILE, "r"); |
---|
42 | if (fp == NULL) |
---|
43 | return 75; |
---|
44 | { |
---|
45 | char buf[10]; |
---|
46 | if (!(fread (buf, 1, 10, fp) == 9)) |
---|
47 | { fclose (fp); return 10; } |
---|
48 | if (!(memcmp (buf, "foogarsh!", 9) == 0)) |
---|
49 | { fclose (fp); return 11; } |
---|
50 | } |
---|
51 | if (!(fclose (fp) == 0)) |
---|
52 | return 12; |
---|
53 | |
---|
54 | /* The file's contents is now "foogarsh!". */ |
---|
55 | |
---|
56 | return 0; |
---|
57 | } |
---|