title: convert Ascii code file into binary file
body: <p>I have a problem of converting ASCII code file into binary file. The code is able to read file and print data into other file as ASII code ( using fprintf); however, when I try to convert from ASII code to binary file ( using fwrite), and  then use (fread) the file again. It doesn't generate the right answer. My doubt is the fwrite function doesn't work probably. Could you please advise on how to fix this problem? Thank you very much.</p>
  
  <pre><code>============================================
  Code to convert from ASCII to binary file
  ===========================================
  # include &lt;stdio.h&gt;
  # include &lt;stdlib.h&gt;
  # include &lt;string.h&gt;
  
  typedef struct _FileData
  {
  int a;
  double b;
  char dataStr[56];
   }FileData;
  
  
  int main()
  {
  // open read file   
  FILE * infile=fopen("output.txt", "r");
  if(infile==NULL)
  {
      printf("Error opening file");
      exit(1);
  }
  
  // open write file
  FILE * outfile = fopen("out_file.txt","wb");
  if( outfile==NULL)
  {
      printf("Error writting on file");
      exit(1);
  }
  
  FileData input; // pointer for read file
  FileData output; // pointer for write file
  
  while( fscanf(infile,"%d %lf %[^\n]s",&amp;input.a,&amp;input.b,&amp;input.dataStr)==3)
  {
      /*printf("%d\n",input.a);
      printf("%.3lf\n",input.b);
      printf("%s\n",input.dataStr);*/
      //fprintf(outfile,"%d\n %.3lf\n %s\n",input.a,input.b,input.dataStr);
      fwrite(&amp;output,sizeof(FileData),1,outfile);
  
  
  }
  fclose(infile);
  fclose(outfile);
  return 0;
     }
  ==============================================================
  Code to convert from binary file to ASCII code file
  =============================================================
  # include &lt;stdio.h&gt;
  # include &lt;stdlib.h&gt;
  # include &lt;string.h&gt;
  
  typedef struct FileData
  {
  int a;
  double b;
  char dataStr[56];
  }FileData;
  int main()
  {
  
  FILE * infile=fopen("out_file.txt", "rb");
  if(infile==NULL)
  {
   printf("Error opening file");
   }
  FileData input;
  while(fread(&amp;input,sizeof(struct FileData),sizeof(struct FileData),infile))
  {
  printf("%d\n",input.a);
  printf("%.3f\n",input.b);
  printf("%s\n",input.dataStr);
  }
  
  return 0;
  }
  
  ===========================================================
  Data 
  ==========================================================
  
  47
  34.278
  This is a line of text
  
  48
  23.678
  This a very very long line
  </code></pre>