1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*===================================================================== |
6 | ** |
7 | ** Source: test5.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the fopen function. |
10 | ** Test to ensure that you can write to a 'r+' mode file. |
11 | ** And that you can read from a 'r+' mode file. |
12 | ** |
13 | ** Depends: |
14 | ** fprintf |
15 | ** fclose |
16 | ** fgets |
17 | ** fseek |
18 | ** |
19 | |
20 | ** |
21 | **===================================================================*/ |
22 | |
23 | #include <palsuite.h> |
24 | |
25 | int __cdecl main(int argc, char **argv) |
26 | { |
27 | |
28 | FILE *fp; |
29 | char buffer[128]; |
30 | |
31 | if (PAL_Initialize(argc, argv)) |
32 | { |
33 | return FAIL; |
34 | } |
35 | |
36 | |
37 | /* Open a file with 'w' mode */ |
38 | if( (fp = fopen( "testfile" , "w" )) == NULL ) |
39 | { |
40 | Fail( "ERROR: The file failed to open with 'w' mode.\n" ); |
41 | } |
42 | |
43 | if(fclose(fp)) |
44 | { |
45 | Fail("ERROR: Attempted to close a file, but fclose failed. " |
46 | "This test depends upon it." ); |
47 | } |
48 | |
49 | if( (fp = fopen( "testfile" , "r+" )) == NULL ) |
50 | { |
51 | Fail( "ERROR: The file failed to open with 'r+' mode.\n" ); |
52 | } |
53 | |
54 | /* Write some text to the file */ |
55 | if(fprintf(fp,"%s" ,"some text" ) <= 0) |
56 | { |
57 | Fail("ERROR: Attempted to WRITE to a file opened with 'r+' mode " |
58 | "but fprintf failed. Either fopen or fprintf have problems." ); |
59 | } |
60 | |
61 | if(fseek(fp, 0, SEEK_SET)) |
62 | { |
63 | Fail("ERROR: fseek failed, and this test depends on it." ); |
64 | } |
65 | |
66 | /* Attempt to read from the 'r+' only file, should pass */ |
67 | if(fgets(buffer,10,fp) == NULL) |
68 | { |
69 | Fail("ERROR: Tried to READ from a file with 'r+' mode set. " |
70 | "This should succeed, but fgets returned NULL. Either fgets " |
71 | "or fopen is broken." ); |
72 | } |
73 | |
74 | PAL_Terminate(); |
75 | return PASS; |
76 | } |
77 | |
78 | |
79 | |