954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fread, fwrite issues

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;
}
degamer106
Junior Poster
131 posts since Mar 2006
Reputation Points: 10
Solved Threads: 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...

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 

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;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

degamer106
Junior Poster
131 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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.

degamer106
Junior Poster
131 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

read the description of the open flags -- one of the allows both read and writes and your program only opens the file once.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
I know its possible to write binary files because I've done it hundreds of times, and so has almost everyone else.

Writing structures as text files may be preferred for portability and human read/writeability. http://c-faq.com/struct/io.html

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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 :)

degamer106
Junior Poster
131 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You