Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il
Hostgator 1 Penny

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


Distribute your Resume to 500+ Job Boards & Recruiters


6,849 questions

9,751 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
...