summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/ferror/test2/test2.cpp
blob: fdf9e032c854060243d961fd276c2d132a5c40f3 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  test2.c
**
** Purpose: Open a read-only file and attempt to write some data to it.
** Check to ensure that an ferror occurs.
**
** Depends:
**     fopen
**     fwrite
**     fclose
**     
**
**
**===================================================================*/

#include <palsuite.h>

int __cdecl main(int argc, char **argv)
{
    const char filename[] = "testfile";
    FILE * fp = NULL;
    int result;

    if (PAL_Initialize(argc, argv))
    {
        return FAIL;
    }

    /* Open a file in READONLY mode */
  
    if((fp = fopen(filename, "r")) == NULL)
    {
        Fail("Unable to open a file for reading.");
    }

    /* Attempt to write 14 characters to the file. */
  
    if((result = fwrite("This is a test",1,14,fp)) != 0)
    {
        Fail("ERROR: %d characters written.  0 characters should "
             "have been written, since this file is read-only.", result);
    }
  
    if(ferror(fp) == 0)
    {
        Fail("ERROR:  ferror should have generated an error when "
             "write was called on a read-only file.  But, it "
             "retured 0, indicating no error.\n");
    }
  
    /* Close the file. */

    if(fclose(fp) != 0)
    {
        Fail("ERROR: fclose failed when trying to close a file pointer. "
	     "This test depends on fclose working properly.");
    }

   
    PAL_Terminate();
    return PASS;
}