summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/fputs/test1/test1.c
blob: b90ea082e92a1ec036a39a7e2de3a02482224892 (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
// 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:  Call fputs twice and write two strings to a file.  Then
** call fread on the file and check that the data which was written is what
** we expect it to be.
**  

**
**===================================================================*/

                                 
#include <palsuite.h>


int __cdecl main(int argc, char **argv)
{
    
    FILE* TheFile;
    char* StringOne = "FooBar";
    char* StringTwo = "BarFoo";
    char* CompleteString = "FooBarBarFoo";
    char ReadBuffer[64];
    int ret;

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

    /* Open the file that we'll be working with */

    TheFile = fopen("TestFile", "w+");
  
    if(TheFile == NULL)
    {
        Fail("ERROR: fopen failed to open the file 'TestFile' in read/write "
             "mode.\n");
    }

    /* Call fputs twice to write two strings to the file stream */
    
    if(fputs(StringOne, TheFile) < 0)
    {
        Fail("ERROR: fputs returned a negative value when attempting to "
             "put the string '%s' to the file.\n",StringOne);
    }
    
    if(fputs(StringTwo, TheFile) < 0)
    {
        Fail("ERROR: fputs returned a negative value when attempting to "
             "put the string '%s' to the file.\n",StringTwo);
    }
    
    /* Flush the buffers */
    if(fflush(TheFile) != 0)
    {
        Fail("ERROR: fflush failed to properly flush the buffers.\n");
    }

    /* Now read from the file to ensure the data was written correctly. 
       Note: We read more than what was written to make sure nothing extra
       was written.
    */

    if(fseek(TheFile, 0, SEEK_SET) != 0)
    {
        Fail("ERROR: fseek failed to set the file pointer back to the start "
             "of the file.\n");
    }


    if((ret = fread(ReadBuffer, 1, 20, TheFile)) != 12)
    {
        Fail("ERROR: fread should have returned that it read in 12 characters "
             "from the file, but instead it returned %d.\n", ret);
    }

    ReadBuffer[ret] = '\0';

    if(strcmp(ReadBuffer, CompleteString) != 0)
    {
        Fail("ERROR: The data read back from the file is not exactly the same "
             "as the data that was written by fputs.  The file contains '%s' "
             "instead of '%s'.\n",ReadBuffer, CompleteString);
    }

    if(fclose(TheFile) != 0)
    {
        Fail("ERROR: fclose failed to close the file stream.\n");
    }
     
    PAL_Terminate();
    return PASS;
}