OldSchoolCode

.com



Binary files
So to read and write a text file you open the file like this
//read
FILE *pFile = fopen( fileName, "r" );

//write
FILE *pFile = fopen( fileName, "w" );

//append
FILE *pFile = fopen( fileName, "a" );
this tells the file writing functions to write everything to the file as if it were an ascii character. It's great for small things, but not incredibly useful for big files that contain a lot of data.

If we open the file for binary writing, it tells the file writing functions to write the exact bytes in the memory location to the file. so you get a carbon copy of your memory imprint at a certain location.

to open a file for binary writing, simply use a 'b' after the 'r', 'w', or 'a'
//read binary
FILE *pFile = fopen( fileName, "rb" );

//write binary
FILE *pFile = fopen( fileName, "wb" );

//append binary
FILE *pFile = fopen( fileName, "ab" );

//to write data to a file you use fwrite...
//fwrites declaration looks like this...
//fwrite returns the amount of items written
size_t fwrite(
   //this is the address of data you want to write
   const void *pBuffer, 
   //this is how far from the address you want to write
   size_t size, 
   //this is how many of it you wish to write (always have this 1)      
   size_t count, 
   //this is a pointer to the file that is open
   FILE *pFile
);
first note: "size_t" is the same as an "unsigned int". the "unsigned" means it can't go below 0. second note: you're going to see a function called sizeof. This returns the amount of bytes of what is passed to it. For instance, sizeof(Person), would return the number of bytes the Person struct consumes.
//pretend we are using an instance of our Person struct
//which we named customer, and we have already filled out 
//the data in customer
fwrite( &customer, sizeof(Person), 1, pFile );
so this command tells fwrite to start writing at the memory address of "customer". (notice we use the addressof operator(&)). and it tells it to write out every subsequent memory address up to the total bytes Person takes up. sizeof(Person).

ok now remember pointers already point to the memory address, so if you use a pointer with fwrite you don't need to put the address of operator infront of it.
Person *pCustomer = &customer;
fwrite( pCustomer, sizeof(Person), 1, pFile );

//remember to close the file when you are done writing to it
fclose( pFile );
ok - now you know how to write to a file. If you then open the file in notepad you will see it is a bunch of binary data that interprets as weird ascii characters.

so to read from the file you use fread. it is very similar as fwrite. Lets say you've already opened the file for binary read and now you want to read the contents of it. You know that you previously wrote a person struct to the file, so now you want to read it back out.
Person customer;
fread( &customer, sizeof(Person), 1, pFile );
this command will start reading the bytes from the file and place them into the memory address which "customer" is located at. once this function is done, customer will have valid data in it that was saved out to a file :)

__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE

#include <string.h>
#include <stdio.h>
#include <conio.h>
   
struct Person
{
   char name[ 64 ];
   int age;
   char sex;
   int weight;
};
      
void main( void )
{
   int count = 0;
   int stop;
   
   char name[ 128 ] = "binary"; 
   char fileName[ 128 ];
	
   Person customer[ 2 ];
	
   do
   {
      Person *pCustomer = &customer[ i ];
		
      printf( "\n Enter name :" );
      scanf( "%s", &pCustomer->name );
      fflush(stdin);

      printf( "\n Enter age :" );
      scanf( "%d", &pCustomer->age );
      fflush( stdin );
   
      printf( "\n Enter sex, M or F :" );
      scanf( "%c", &pCustomer->sex );
      fflush( stdin );
      
      printf( "\n Enter weight :" );
      scanf( "%d", &pCustomer->weight );
      fflush( stdin );

      count++;
   }
   while ( count < 2 );
	   
   sprintf( fileName, "C:\\%s.txt", name );

   //write all our customers to a file
   FILE *pFile = fopen( fileName, "ab" );	
   fwrite( &customer, sizeof(Person) * count, 1, pFile );

   fclose( pFile );

   getch( );
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE

#include <string.h>
#include <stdio.h>
#include <conio.h>
   
struct Person
{
   char name[ 64 ];
   int age;
   char sex;
   int weight;
};


void PrintCustomerStruct( Person *pCustomer, int count)
{
   int i;
	
   for ( i = 0; i < count; i++ )
   {
       printf( "\n Name : %s", pCustomer[ i ].name );
       printf( "\n Age : %d", pCustomer[ i ].age );
       printf( "\n Sex: %c", pCustomer[ i ].sex );
       printf( "\n Weight : %d", pCustomer[ i ].weight );
   }
}
      
void main( void )
{
   int i = 0;
   int stop;
   
   char name[ 128 ]; 
   char fileName[ 128 ];
	
   Person customer[ 2 ];
   
   printf( " Enter file name :" );

   scanf( "%s", name );

   sprintf( filename, "C:\\%s.txt", name );
   
   FILE *pFile = fopen( filename, "rb" );

   if ( pFile == 0 )
   {
      printf( "\n\n File does not exist in this directory." );
   }

   else
   {
      fread( customer, sizeof(Person) * 2, 1, pFile );
	  
      PrintCustomerStruct( customer, 2 );
	   
      fclose( pFile );
   }
	
   getch( );
}