One of the things I can't get working is reading and writing a structure to and from a file in binary. I'm sure that I'm using fread and fwrite correctly. However, I keep getting crashes which I cannot find ways to debug.

#include <stdio.h>

#define MAX	1

typedef struct {
	short int key;
	char name[21];
	char symbol[6];
	float price;
	float high;
	float low;
	short int ratio;
} STOCK;

void get_data(STOCK *);

int main(void) {

	FILE *fp;
//	STOCK data[MAX];
	STOCK data[MAX] = {	{
			{1000}, {"nasdaq"}, {"NAS"}, {100}, {100}, {50}, {2}
		}
	};

	if (!(fp = fopen("stockdata.dat", "rb"))) {
//		get_data(data);
		FILE *temp = fopen("stockdata.dat", "wb");
		fwrite(data, sizeof(STOCK), MAX, temp);
		fclose(temp);
	}

	STOCK test[MAX];
	fread(test, sizeof(STOCK), MAX, fp);
	printf("%hd", test[0].key);

	return 0;
}

If you want to write array of structures in file you should use loop to store each member. Coinsider this code:

#include <string.h>
#define LEN 5

typedef struct Test
{
    int x;
    double y;
}Test;


int main(void)
{
    Test array[LEN];
    Test res[LEN];
    FILE* fp;
    int i;
    /*Open for writing*/
    fp = fopen("test.bin","wb");
    for (i = 0; i <LEN; i++)
    {
        array[i].x = i;
        array[i].y =  i * 2.5;
        fwrite(&array[i], sizeof(Test), 1, fp);
    }
    fclose(fp);
    /* Open for reading*/
    fp = fopen("test.bin", "rb");
    for (i = 0; i <LEN; i++)
    {
        fread(&res[i], sizeof(Test), 1, fp);
    }
    for (i = 0; i <LEN; i++)
    {
        printf ("%d %g\n", res[i].x, res[i].y);
    }
    fclose(fp);
    
    return 0;
}

Now, I think you'll know what to do...

I know its possible to write binary files because I've done it hundreds of times, and so has almost everyone else.

This works for me.

#include <stdio.h>

#define MAX	1

typedef struct {
	short int key;
	char name[21];
	char symbol[6];
	float price;
	float high;
	float low;
	short int ratio;
} STOCK;

void get_data(STOCK *);

int main(void) {

	FILE *fp;
	STOCK data[MAX] = {	{
			{1000}, {"nasdaq"}, {"NAS"}, {100}, {100}, {50}, {2}
		}
	};
	// if the file does not exist, then create it
	if(!(fp = fopen("stockdata.dat", "rb")) )
	{
		if(fp = fopen("stockdata.dat", "wb")) { 
			fwrite(data, sizeof(STOCK), MAX, fp);
			fclose(fp);
			fp = fopen("stockdata.dat", "rb");
		}
		else
		{
			printf("can't write to file\n");
			return 1;
		}
	}

	STOCK test[MAX];
	fread(test, sizeof(STOCK), MAX, fp);
	fclose(fp);
	printf("%hd\n", test[0].key);

	return 0;
}

so you're allowed to change the state of the file by pointing the pointer elsewhere?

How would I copy data from one file directly to another? I tried this syntax:

void disp_all(FILE *fp) {

	FILE *ftemp = fopen("tempdata.dat", "wb");

	fwrite(fp, sizeof(STOCK), 1, ftemp);
	fclose(ftemp);
	ftemp = fopen("tempdata.dat", "rb");

	STOCK temp[MAX];
	fread(temp, sizeof(STOCK), 1, ftemp);
	printf("%hd\n", temp[0].key);

	rewind(fp);
}

and it doesn't seem to work :sad:

fp is in "rb" mode. I want to write the file that fp is pointing to directly into a temporary file that ftemp is pointing to.

ok, i read the website that ancient dragon posted up and it was pretty helpful. I managed to get the file copying function of my program to work :)

This article has been dead for over six months. Start a new discussion instead.