summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/fwrite/test1/test1.c
blob: 392522879fdc265da10c154a919bac7d6147920c (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 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:  test1.c
**
** Purpose: Write a short string to a file and check that it was written
**          properly.
**
**
**==========================================================================*/

#include <palsuite.h>

int __cdecl main(int argc, char **argv)
{
    const char filename[] = "testfile.tmp";
    const char outBuffer[] = "This is a test.";
    char inBuffer[sizeof(outBuffer) + 10];
    int itemsExpected;
    int itemsWritten;
    FILE * fp = NULL;
  
    if (PAL_Initialize(argc, argv))
    {
        return FAIL;
    }

    if((fp = fopen(filename, "w")) == NULL)
    {
        Fail("Unable to open a file for write.\n");
    }

    itemsExpected = sizeof(outBuffer);
    itemsWritten = fwrite(outBuffer, 
                          sizeof(outBuffer[0]), 
                          sizeof(outBuffer), 
                          fp);  
    
    if (itemsWritten == 0)
    {
        if(fclose(fp) != 0)
        {
            Fail("fwrite: Error occurred during the closing of a file.\n");
        }

        Fail("fwrite() couldn't write to a stream at all\n");
    }
    else if (itemsWritten != itemsExpected) 
    {
        if(fclose(fp) != 0)
        {
            Fail("fwrite: Error occurred during the closing of a file.\n");
        }

        Fail("fwrite() produced errors writing to a stream.\n");
    }
      
    if(fclose(fp) != 0)
    {
        Fail("fwrite: Error occurred during the closing of a file.\n");
    }

    /* open the file to verify what was written to the file */
    if ((fp = fopen(filename, "r")) == NULL)
    {
        Fail("Couldn't open newly written file for read.\n");
    }

    if (fgets(inBuffer, sizeof(inBuffer), fp) == NULL)
    {
        if(fclose(fp) != 0)
        {
            Fail("fwrite: Error occurred during the closing of a file.\n");
        }

        Fail("We wrote something to a file using fwrite() and got errors"
             " when we tried to read it back using fgets().  Either "
             "fwrite() or fgets() is broken.\n");
    }

    if (strcmp(inBuffer, outBuffer) != 0)
    {
        if(fclose(fp) != 0)
        {
            Fail("fwrite: Error occurred during the closing of a file.\n");
        }

        Fail("fwrite() (or fgets()) is broken.  The string read back from"
             " the file does not match the string written.\n");
    }

    if(fclose(fp) != 0)
    {
        Fail("fwrite: Error occurred during the closing of a file.\n");
    }

    PAL_Terminate();
    return PASS;
}