Write data to a file with format - C File

C examples for File:Binary File

Description

Write data to a file with format

Demo Code

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   long num1 = 234567L;
   long num2 = 345123L;
   long num3 = 789234L;

   long num4 = 0L;                            // Values read from the file. . .
   long num5 = 0L;
   long num6 = 0L;

   float fnum = 0.0f;                         // Value read from the file
   int   ival[6] = { 0 };                     // Values read from the file
   FILE *pfile = NULL;                        // File pointer
   char *filename = "myfile.txt";

   pfile = fopen(filename, "w");

   if (pfile == NULL) {
      printf("Error opening %s for writing. Program terminated.\n", filename);
      exit(1);/*from   w w  w .  ja va2s .com*/
   }
   setbuf(pfile, NULL);

   fprintf(pfile, "%6ld%6ld%6ld", num1, num2, num3);          // Write file
   fclose(pfile);                                               // Close file
   printf(" %6ld %6ld %6ld\n", num1, num2, num3);             // Display values written

   pfile = fopen(filename, "r");

   if (pfile == NULL) {
      printf("Error opening %s for reading. Program terminated.\n", filename);
      exit(1);
   }
   setbuf(pfile, NULL);

   fscanf(pfile, "%6ld%6ld%6ld", &num4, &num5, &num6);        // Read back
   printf(" %6ld %6ld %6ld\n", num4, num5, num6);             // Display what we got

   rewind(pfile);                             // Go to the beginning of the file
   fscanf(pfile, "%2d%3d%3d%3d%2d%2d%3f", &ival[0], &ival[1], &ival[2], &ival[3], &ival[4], &ival[5], &fnum);
   fclose(pfile);
   remove(filename);

   // Output the results
   printf("\n");
   for (size_t i = 0; i < sizeof(ival) / sizeof(ival[0]); ++i)
      printf("%sival[%zd] = %d", i == 4 ? "\n\t" : "\t", i, ival[i]);
   printf("\nfnum = %f\n", fnum);
   return 0;
}

Result


Related Tutorials