#include <stdio.h>
#include <string.h>
const unsigned char *p = "This is some text and a number: 12345678";
const unsigned char *filename = "somefile.bin";
#define MAX_LENGTH 80
//*******************************************************************
//*******************************************************************
void DofwriteChars(void)
{
FILE *outfile;
// Open the binary file
outfile = fopen(filename, "wb");
// Opened OK
if (outfile)
{
// Write characters to the file
int count = fwrite(p, sizeof(char), strlen(p), outfile);
printf("%i elements written\n", count);
// Close the file
fclose(outfile);
}
else
perror(filename); // Couldn't open the file
}
//*******************************************************************
//*******************************************************************
void DofreadChars(void)
{
FILE *infile;
// Open the binary file
infile = fopen(filename, "rb");
// Opened OK
if (infile)
{
char buffer[MAX_LENGTH];
// Read in a max of MAX_LENGTH - 1 characters
int count = fread(buffer, sizeof(unsigned char), MAX_LENGTH - 1, infile);
printf("%i elements read\n", count);
// fread does NOT automatically append a NULL byte
buffer[count] = 0;
printf("%s\n", buffer);
// Close the file
fclose(infile);
}
else
perror(filename); // couldn't open the file
}
#define NUM_INTS 5
//*******************************************************************
//*******************************************************************
void DofwriteInts(void)
{
FILE *outfile;
// Open the binary file
outfile = fopen(filename, "wb");
// Opened OK
if (outfile)
{
int i, count = 0;
// Write some integers to the file
for (i = 0x12345678; i < 0x12345678 + NUM_INTS; i++)
count += fwrite(&i, sizeof(int), 1, outfile);
printf("%i elements written\n", count);
// Close the file
fclose(outfile);
}
else
perror(filename); // couldn't open the file
}
//*******************************************************************
//*******************************************************************
void DofreadInts(void)
{
FILE *infile;
// Open the binary file
infile = fopen(filename, "rb");
// Opened OK
if (infile)
{
int i, x;
// Read integers from file
for (i = 0; i < NUM_INTS; i++)
{
fread(&x, sizeof(int), 1, infile);
printf("%X\n", x);
}
// Close the file
fclose(infile);
}
else
perror(filename); // couldn't open the file
}
/*
The above function prints out:
12345678
12345679
1234567A
1234567B
1234567C
*/
//*******************************************************************
//*******************************************************************
// structs
struct INFO
{
int i;
char c;
double d;
};
#define NUM_DATA 4
struct INFO Data[NUM_DATA] = {{ 1, 'A', 1.1},
{11, 'B', 2.2},
{22, 'C', 3.3},
{33, 'D', 4.4}
};
//*******************************************************************
//*******************************************************************
void DofwriteStruct1(void)
{
// Open the binary file
FILE *outfile = fopen(filename, "wb");
// Opened OK
if (outfile)
{
// Write each field individually to the file
fwrite(&Data[0].i , sizeof(int), 1, outfile);
fwrite(&Data[0].c , sizeof(char), 1, outfile);
fwrite(&Data[0].d , sizeof(double), 1, outfile);
fclose(outfile);
}
else
perror(filename); // couldn't open the file
}
//*******************************************************************
//*******************************************************************
void DofwriteStruct2(void)
{
// Open the binary file
FILE *outfile = fopen(filename, "wb");
// Opened OK
if (outfile)
{
// Write the entire struct to the file
fwrite(&Data[0], sizeof(struct INFO), 1, outfile);
fclose(outfile);
}
else
perror(filename); // couldn't open the file
}
//*******************************************************************
//*******************************************************************
void DofwriteStructs(void)
{
// Open the binary file
FILE *outfile = fopen(filename, "wb");
// Opened OK
if (outfile)
{
// The array of structs to the file
fwrite(Data, sizeof(struct INFO), NUM_DATA, outfile);
fclose(outfile);
}
else
perror(filename); // couldn't open the file
}
//*******************************************************************
//*******************************************************************
void DofreadStructs(void)
{
// Open the binary file
FILE *infile = fopen(filename, "rb");
// Opened OK
if (infile)
{
int i, count;
struct INFO Data[4];
// Read structs from the file into the array
count = fread(Data, sizeof(struct INFO), NUM_DATA, infile);
printf("%i structs read\n", count);
// Display them
for (i = 0; i < count; i++)
printf("struct #%i: i=%i, c=%c, d=%g\n", i, Data[i].i, Data[i].c, Data[i].d);
// Close the file
fclose(infile);
}
else
perror(filename); // couldn't open the file
}
File Contents
Individual fields (DofwriteStruct1)
Entire struct (DofwriteStruct2)01 00 00 00 41 9A 99 99 99 99 99 F1 3F ....Aš™™™™™ñ?
01 00 00 00 41 00 00 00 9A 99 99 99 99 99 F1 3F ....A...š™™™™™ñ?