| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*===================================================================== |
| 6 | ** |
| 7 | ** Source: test1.c |
| 8 | ** |
| 9 | ** Purpose: Call fputs twice and write two strings to a file. Then |
| 10 | ** call fread on the file and check that the data which was written is what |
| 11 | ** we expect it to be. |
| 12 | ** |
| 13 | |
| 14 | ** |
| 15 | **===================================================================*/ |
| 16 | |
| 17 | |
| 18 | #include <palsuite.h> |
| 19 | |
| 20 | |
| 21 | int __cdecl main(int argc, char **argv) |
| 22 | { |
| 23 | |
| 24 | FILE* TheFile; |
| 25 | char* StringOne = "FooBar" ; |
| 26 | char* StringTwo = "BarFoo" ; |
| 27 | char* CompleteString = "FooBarBarFoo" ; |
| 28 | char ReadBuffer[64]; |
| 29 | int ret; |
| 30 | |
| 31 | if (PAL_Initialize(argc, argv)) |
| 32 | { |
| 33 | return FAIL; |
| 34 | } |
| 35 | |
| 36 | /* Open the file that we'll be working with */ |
| 37 | |
| 38 | TheFile = fopen("TestFile" , "w+" ); |
| 39 | |
| 40 | if(TheFile == NULL) |
| 41 | { |
| 42 | Fail("ERROR: fopen failed to open the file 'TestFile' in read/write " |
| 43 | "mode.\n" ); |
| 44 | } |
| 45 | |
| 46 | /* Call fputs twice to write two strings to the file stream */ |
| 47 | |
| 48 | if(fputs(StringOne, TheFile) < 0) |
| 49 | { |
| 50 | Fail("ERROR: fputs returned a negative value when attempting to " |
| 51 | "put the string '%s' to the file.\n" ,StringOne); |
| 52 | } |
| 53 | |
| 54 | if(fputs(StringTwo, TheFile) < 0) |
| 55 | { |
| 56 | Fail("ERROR: fputs returned a negative value when attempting to " |
| 57 | "put the string '%s' to the file.\n" ,StringTwo); |
| 58 | } |
| 59 | |
| 60 | /* Flush the buffers */ |
| 61 | if(fflush(TheFile) != 0) |
| 62 | { |
| 63 | Fail("ERROR: fflush failed to properly flush the buffers.\n" ); |
| 64 | } |
| 65 | |
| 66 | /* Now read from the file to ensure the data was written correctly. |
| 67 | Note: We read more than what was written to make sure nothing extra |
| 68 | was written. |
| 69 | */ |
| 70 | |
| 71 | if(fseek(TheFile, 0, SEEK_SET) != 0) |
| 72 | { |
| 73 | Fail("ERROR: fseek failed to set the file pointer back to the start " |
| 74 | "of the file.\n" ); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | if((ret = fread(ReadBuffer, 1, 20, TheFile)) != 12) |
| 79 | { |
| 80 | Fail("ERROR: fread should have returned that it read in 12 characters " |
| 81 | "from the file, but instead it returned %d.\n" , ret); |
| 82 | } |
| 83 | |
| 84 | ReadBuffer[ret] = '\0'; |
| 85 | |
| 86 | if(strcmp(ReadBuffer, CompleteString) != 0) |
| 87 | { |
| 88 | Fail("ERROR: The data read back from the file is not exactly the same " |
| 89 | "as the data that was written by fputs. The file contains '%s' " |
| 90 | "instead of '%s'.\n" ,ReadBuffer, CompleteString); |
| 91 | } |
| 92 | |
| 93 | if(fclose(TheFile) != 0) |
| 94 | { |
| 95 | Fail("ERROR: fclose failed to close the file stream.\n" ); |
| 96 | } |
| 97 | |
| 98 | PAL_Terminate(); |
| 99 | return PASS; |
| 100 | } |
| 101 | |