diff options
Diffstat (limited to 'src/pal/tests/palsuite/c_runtime/ferror/test2/test2.c')
-rw-r--r-- | src/pal/tests/palsuite/c_runtime/ferror/test2/test2.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/c_runtime/ferror/test2/test2.c b/src/pal/tests/palsuite/c_runtime/ferror/test2/test2.c new file mode 100644 index 0000000000..fdf9e032c8 --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/ferror/test2/test2.c @@ -0,0 +1,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; +} + + |