0098-tmpfil.c (989B)
1 #include <assert.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 /* 6 output: 7 testing 8 test1 9 test2 10 done 11 end: 12 */ 13 14 static void 15 test1(void) 16 { 17 char buf[80]; 18 FILE *fp1, *fp2; 19 20 puts("test1"); 21 fp1 = tmpfile(); 22 assert(fp1); 23 fp2 = tmpfile(); 24 assert(fp2); 25 26 fputs("This is the first\n", fp1); 27 fputs("This is the second\n", fp2); 28 fflush(fp1); 29 fflush(fp2); 30 31 rewind(fp1); 32 rewind(fp2); 33 34 fgets(buf, sizeof(buf), fp1); 35 assert(strcmp(buf, "This is the first\n") == 0); 36 37 fgets(buf, sizeof(buf), fp2); 38 assert(strcmp(buf, "This is the second\n") == 0); 39 40 assert(fclose(fp1) == 0); 41 assert(fclose(fp2) == 0); 42 } 43 44 static void 45 test2(void) 46 { 47 int i, max; 48 FILE *fp; 49 static FILE *fps[FOPEN_MAX]; 50 51 puts("test2"); 52 53 max = TMP_MAX - 2; 54 if (max > FOPEN_MAX) 55 max = FOPEN_MAX; 56 if (max > 32) 57 max = 32; 58 59 for (i = 0; i < max; i++) 60 assert(fps[i] = tmpfile()); 61 62 for (i = 0; i < max; i++) 63 assert(fclose(fps[i]) == 0); 64 } 65 66 int 67 main(void) 68 { 69 puts("testing"); 70 test1(); 71 test2(); 72 puts("done"); 73 74 return 0; 75 }