summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/fgets/test3/test3.cpp
blob: 525ba9327f4615a16ebb3c7a470770842c9b4a43 (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
70
71
72
73
// 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:  test3.c
**
** Purpose: Tries to read from an empty file using fgets(), to verify
**          handling of EOF condition.
**
**
**==========================================================================*/

#include <palsuite.h>

int __cdecl main(int argc, char **argv)
{
    char inBuf[10];
    const char filename[] = "testfile.tmp";

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


    /*write the empty file that we will use to test */
    fp = fopen(filename, "w");
    if (fp == NULL)
    {
        Fail("Unable to open file for write.\n");
    }

    /*Don't write anything*/
  
    if (fclose(fp) != 0) 
    {
        Fail("Error closing stream opened for write.\n");
    }


    /*Open the file and try to read.*/
    fp = fopen(filename, "r");
    if (fp == NULL)
    {
        Fail("Unable to open file for read.\n");
    }

  
    if (fgets(inBuf, sizeof(inBuf) , fp) != NULL)
    {
        /*NULL could also mean an error condition, but since the PAL
          doesn't supply feof or ferror, we can't distinguish between
          the two.*/
        Fail("fgets doesn't handle EOF properly.  When asked to read from "
             "an empty file, it didn't return NULL as it should have.\n");
    }

    if (fclose(fp) != 0)
    {
        Fail("Error closing an empty file after trying to use fgets().\n");
    }
    PAL_Terminate();
    return PASS;

}