Mega Code Archive

 
Categories / C / File
 

Save and write different type of value into file

#include <stdio.h> #include <stdlib.h> int main(void) {   FILE *fp;   int i;   /* open file for output */   if((fp = fopen("my.txt", "wb"))==NULL) {     printf("Cannot open file.\n");     exit(1);   }   i = 10;   if(fwrite(&i, sizeof(int), 1, fp) != 1) {     printf("Write error occurred.\n");     exit(1);   }   fclose(fp);   /* open file for input */   if((fp = fopen("my.txt", "rb"))==NULL) {     printf("Cannot open file.\n");     exit(1);   }   if(fread(&i, sizeof i, 1, fp) != 1) {     printf("Read error occurred.\n");     exit(1);   }   printf("i is %d",i);   fclose(fp);   return 0; }