Back to index

glibc  2.9
tst-fwrite.c
Go to the documentation of this file.
00001 /* Derived from the test case in
00002    http://sourceware.org/bugzilla/show_bug.cgi?id=1078.  */
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <unistd.h>
00006 
00007 #define OUT_SIZE 10000
00008 
00009 
00010 static int fd;
00011 
00012 static void prepare (void);
00013 #define PREPARE(argc, argv) prepare ()
00014 
00015 static int do_test (void);
00016 #define TEST_FUNCTION do_test ()
00017 
00018 #include "../test-skeleton.c"
00019 
00020 
00021 static void
00022 prepare (void)
00023 {
00024   fd = create_temp_file ("tst-fwrite.", NULL);
00025   if (fd == -1)
00026     {
00027       puts ("cannot create temporary file");
00028       exit (1);
00029     }
00030 }
00031 
00032 
00033 static int
00034 do_test (void)
00035 {
00036   FILE* f = fdopen (fd, "w+");
00037   if (f == NULL) {
00038     puts ("cannot create stream");
00039     return 1;
00040   }
00041   puts ("Opened temp file");
00042 
00043   if (fwrite ("a", 1, 1, f) != 1)
00044     {
00045       puts ("1st fwrite failed");
00046       return 1;
00047     }
00048   puts ("Wrote a byte");
00049   fflush (f);
00050 
00051   char buffer[10000];
00052   size_t i = fread (buffer, 1, sizeof (buffer), f);
00053   printf ("Read %zu bytes\n", i);
00054 
00055   for (i = 0; i < OUT_SIZE; i++)
00056     {
00057       if (fwrite ("n", 1, 1, f) != 1)
00058        {
00059          printf ("fwrite in loop round %zu failed\n", i);
00060          return 1;
00061        }
00062 
00063       if ((i + 1) % 1000 == 0)
00064        printf ("wrote %zu bytes ...\n", i + 1);
00065     }
00066 
00067   printf ("Wrote %i bytes [done]\n", OUT_SIZE);
00068 
00069   return 0;
00070 }