Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary files Binary files can not be created by an editor (as with text files) A binary file is created by writing on it from a C-program If a human should.

Similar presentations


Presentation on theme: "Binary files Binary files can not be created by an editor (as with text files) A binary file is created by writing on it from a C-program If a human should."— Presentation transcript:

1 Binary files Binary files can not be created by an editor (as with text files) A binary file is created by writing on it from a C-program If a human should not read the file it saves time and memory to use binary files The internal representation in bits ia written to a binary file Contents of databases are typically binary

2 Example Figure 10.3 (extended) Creating a Binary File of Integers and read them into a vector of integers FILE *binaryp, *inbinp; int i, list[255]; binaryp = fopen("nums.bin", "wb"); for (i = 2; i <= 500; i += 2) fwrite(&i, sizeof (int), 1, binaryp); fclose(binaryp); /* read 250 integers into a vector */ inbinp = fopen(”nums.bin”,”rb”); fread(list, sizeof(int), 250, inbinp); fclose(inbinp); …………

3 Figure 10.4 Outline and Function main for Metals Database Inquiry Program /* * Displays all metals in the database that satisfy the search * parameters specified by the program user. */ #include #include #define MAX_DENSITY 20.0 /*maximum density (g/cm^3)*/ #define MAX_MELT_PT 4000 /*maximum melting point(deg.C)*/ #define MAX_TENS_MOD 350 /*maximum tensile modulus(GPa)*/ #define MAX_DAYS 90 /* maximum days to delivery */ #define STR_SIZ 80 /* number of characters in string*/ typedef struct { /* metal structure type */ char name[STR_SIZ]; double density; /* g/cm^3 */ int melt_pt, /* melting point in degrees C */ tens_mod, /* tensile modulus in GPa */ days_to_deliv; /* days from order to delivery */ } metal_t; typedef struct { /* search parameter bounds type */ char low_name[STR_SIZ], high_name[STR_SIZ]; double low_density, high_density; int low_melt_pt, high_melt_pt, low_tens_mod, high_tens_mod, low_days, high_days; } search_params_t;

4 Fig. 10.4 cont. /* Insert prototypes of other functions needed.*/ /* * Prompts the user to enter the search parameters. */ search_params_t get_params(void); /* * Displays records of all metals in the inventory that satisfy search parameters. */ void display_match(FILE *databasep, /* input - file pointer to binary database file*/ search_params_t params);/* input - search parameter bounds*/ int main(void){ char inv_filename[STR_SIZ]; /*name of inventory file */ FILE *inventoryp; /* inventory file pointer*/ search_params_t params; /* search parameter bounds */ /* Get name of inventory file and open it*/ printf("Enter name of inventory file> "); scanf("%s", inv_filename); fflush(stdin); inventoryp = fopen(inv_filename, "rb"); /* Get the search parameters */ params = get_params(); /*Display all metals that satisfy the search parameters */ display_match(inventoryp, params); return(0); }


Download ppt "Binary files Binary files can not be created by an editor (as with text files) A binary file is created by writing on it from a C-program If a human should."

Similar presentations


Ads by Google