Sign in to follow this  
Angex

C - Binary I/O of unsigned long

Recommended Posts

Angex    884
Hi all, I'm having a problem writing/reading some unsigned long data in a binary file. I write 4 values of size 4 bytes; so I'am expecting the file size to be 16 bytes. But when I read the file back; it reports 64 bytes. I just can't figure out why there is a difference; hope someone else can spot my mistake. Here is the code I'm using:

#include <sys/stat.h>
#include <malloc.h>
#include <string.h>
#include <process.h>

int _tmain(int argc, _TCHAR* argv[]) {

	const unsigned int TOTAL_NUMS = 4;

	const unsigned long NUMBERS [] = { 1L, 5L, 7L, 2L };

	const char * PATHNAME = "C:\\test.bin";

	printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
	printf("Expected file size = %d\n", (TOTAL_NUMS * sizeof(unsigned long)));
	printf("Expected count = %d\n", TOTAL_NUMS);



	FILE * fFile = fopen(PATHNAME, "wb");

	if (fFile == NULL) {
		return 1;
	}

	for (int i = 0; i < TOTAL_NUMS; i++) {
		fwrite(NUMBERS, sizeof(unsigned long), TOTAL_NUMS, fFile);
	}

	fclose(fFile);



	struct stat s;

	memset(&s, 0, sizeof(s));

	stat(PATHNAME, &s);

	const unsigned long fSize = s.st_size;

	printf("\nActual file size = %lu\n", fSize);


	if (fSize <= 0L) {
		return 1;
	}



	unsigned long * nums = (unsigned long *) malloc( fSize );

	if (nums == NULL) {
		return 1;
	}

	memset(nums, 0, fSize);

	fFile = fopen(PATHNAME, "rb");


	if (fFile == NULL) {
		return 1;
	}



	const unsigned int count = (fSize / sizeof(unsigned long));

	const unsigned int actualCount = fread(nums, sizeof(unsigned long), count, fFile);

	printf("Calculated count = %u\n", count);
	printf("Actual count = %u\n\n", actualCount);

	fclose(fFile);



	printf("Contents:\n");

	for (int i = 0; i < actualCount; i++) {
		printf("%d = %lu\n", i, nums[i]);
	}

	printf("\n");



	free(nums);

	system("pause");

	return 0;
}



And this is the output I get:
sizeof(unsigned long) = 4
Expected file size = 16
Expected count = 4

Actual file size = 64
Calculated count = 16
Actual count = 16

Contents:
0 = 1
1 = 5
2 = 7
3 = 2
4 = 1
5 = 5
6 = 7
7 = 2
8 = 1
9 = 5
10 = 7
11 = 2
12 = 1
13 = 5
14 = 7
15 = 2

Press any key to continue . . .


[EDIT] Thanks. I was using source tags but I think the comments were screwing it up. After removing them it posted okay. [Edited by - Angex on December 14, 2008 9:18:23 AM]

Share this post


Link to post
Share on other sites
Gage64    1235
Quote:
Original post by Angex
for (int i = 0; i < TOTAL_NUMS; i++) {
fwrite(NUMBERS, sizeof(unsigned long), TOTAL_NUMS, fFile);
}



There's the problem. You're writing 4 numbers 4 times, so 16 numbers in total.

You probably meant to pass 1 to fwrite instead of TOTAL_NUMS, but actually you can just pass it TOTAL_NUMS and remove the loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this