ORCA-C/Tests/Conformance/C17.7.0.2.CC

82 lines
2.3 KiB
Plaintext
Raw Normal View History

/* Conformance Test 17.7.0.2: Verification of fgets function, binary stream */
#include <stdio.h>
#include <string.h>
int main (void)
{
FILE *f1;
int i;
char string [80] = ""; /* initialize input string to null string */
char *strPtr;
f1 = fopen ("3/tmp", "w+b"); /* create binary file to work on */
if (f1 == NULL)
goto Fail1;
i = fputs ("Every good boy deserves favor\n", f1);
if (i)
goto Fail2;
rewind (f1); /* test reading until '\n' seen */
strPtr = fgets (string, 100, f1);
if (strPtr == NULL)
goto Fail;
if (strcmp ("Every good boy deserves favor\n", string))
goto Fail;
fgetc(f1); /* force an EOF condition */
strPtr = fgets (string, 100, f1); /* test reading with EOF seen before */
if (strPtr != NULL) /* any other characters */
goto Fail;
if (! (feof (f1)) )
goto Fail;
if (strcmp ("Every good boy deserves favor\n", string))
goto Fail;
rewind (f1); /* test reading until n-1 chars seen */
strPtr = fgets (string, 15, f1);
if (strPtr == NULL)
goto Fail;
if (strcmp ("Every good boy", string))
goto Fail;
strPtr = fgets (string, 17, f1); /* ensure fgets moves internal file */
if (strPtr == NULL) /* pointer */
goto Fail;
if (strcmp (" deserves favor\n", string))
goto Fail;
fgetc(f1); /* force an EOF condition */
strPtr = fgets (string, 100, f1); /* test reading with EOF seen before */
if (strPtr != NULL) /* any other characters */
goto Fail;
if (! (feof (f1)) )
goto Fail;
if (strcmp (" deserves favor\n", string))
goto Fail;
i = fclose (f1); /* close file and quit */
if (i)
goto Fail3;
printf ("Passed Conformance Test 17.7.0.2\n");
return 0;
Fail:
printf ("Failed Conformance Test 17.7.0.2\n");
return 0;
Fail1:
printf ("Unable to open temp file for Conformance Test 17.7.0.2\n");
return 0;
Fail2:
printf ("Unable to write to temp file for Conformance Test 17.7.0.2\n");
return 0;
Fail3:
printf ("Unable to close temp file for Conformance Test 17.7.0.2\n");
return 0;
}