Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Trees

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,954 questions

12,356 answers

573 users

How to open and write text to a binary file in C

1 Answer

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

int main(void)
{
   int flag = EXIT_SUCCESS;

   char file_name[] = "d:\\data.bin";
   char str[] = "A text for binary file";
   FILE *fp = fopen(file_name, "wb");
   
   if (fp == NULL) 
   {
     flag = EXIT_FAILURE;
     fprintf(stderr, "fopen() error\n");
   }
   else
   {
     size_t size = sizeof * str;
     size_t total = sizeof str;

     size_t total_written = fwrite(str, size, total, fp); 
     
     if (total_written != total)
     {
       flag = EXIT_FAILURE;
       fprintf(stderr, "fwrite() error: wrote only %I64d from %I64d\n", total_written, total);
     }

     fclose(fp);
   }

   return flag;
}
    
/*
run:
 
  
*/

 




answered Aug 20, 2017 by avibootz
...