Jump to content

  • Log In with Google      Sign In   
  • Create Account

Angex

Member Since 16 May 2004
Offline Last Active Jun 08 2014 03:29 AM

Topics I've Started

C - Binary I/O of unsigned long

14 December 2008 - 01:19 AM

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]

Sharing an instance variable between all instances of a class (C++).

16 August 2004 - 05:58 AM

What I want to do is have a pointer in a super class that once initialised has the same address for all instances of it's subclasses. I thought that I could use the static keyword but I keep getting unresloved external symbol errors during the linking phase. Do demonstrate what I mean I wrote a simple example in java:
public class Gremlin {
    protected static int numberOfGremlins = 0;

    public Gremlin() {
        System.out.println("There is " + (++numberOfGremlins) + " gremlin(s) !\n");
    }
}



public class EvolvedGremlin extends Gremlin {
    protected static int numberOfEvolvedGremlins = 0;

    public EvolvedGremlin() {
        super();
        System.out.println("There is " + (++numberOfEvolvedGremlins) + " evolved gremlin(s) !");
        System.out.println("There is " + numberOfGremlins + " gremlin(s) altogeather !\n");
    }

    public void countGremlins() {
        System.out.println("I counted " + numberOfGremlins + " gremlin(s) !");
    }
}

EvolvedGremlin inherits the nuberOfGremlins from Gremlin, but the value is the same for every instance of EvolvedGremlin. So if I created two EvolvedGremlin objects, and two Gremlin objects, and then got an EvolvedGremlin to count the number of Gremlins the output would be:
There is 1 gremlin(s) !

There is 1 evolved gremlin(s) !
There is 1 gremlin(s) altogeather !

There is 2 gremlin(s) !

There is 3 gremlin(s) !

There is 4 gremlin(s) !

There is 2 evolved gremlin(s) !
There is 4 gremlin(s) altogeather !

I counted 4 gremlin(s) !

However I am at a loss as to how this could be done in C++ ?

PARTNERS