My code so far opens a text file in a particular format and reads in the data into a structure called salesRecord. There is no problem there. The problem arises when I write the data to a new binary file. Here is the structure:

struct salesRecord
{
    int custNo;
    char item[45];
    char id[8];
    char cost[11];
    char qty[7];
};

Then I create an array of salesRecords, read in the data from a text file and store them inside the above structure. I can confirm there is no problem here as I have it output the stored data onto the console and all looks well.

The following code is then executed (where sales is the array of salesRecords):

ofstream outFile("salesbin.bin", ios::out|ios::binary);
outFile.write((char*)sales, sizeof(sales));
outFile.close();

When I open the binary file with Notepad, and also when I try to read and print the data stored in the binary file, everything is fine except for the integer datatype. The item, id, cost and qty data elements are all written correctly, but the custNo data is not written as a number and cannot be read as a number. I thought it could have something to do with casting it as (char*), but I also tried this C method and get the same result:

FILE* fd;
fd=fopen("salesbin.bin","wb");
fwrite((struct salesRecord*) sales, sizeof(struct salesRecord), 196, fd);
fclose(fd);

Any help would greatly be appreciated.

FILE* fd;
fd=fopen("salesbin.bin","wb");
fwrite((struct salesRecord*) sales, sizeof(struct salesRecord), 196, fd);
fclose(fd);

are u using fread() for reading your data.
Its obvious that when u open your file using some editor u wont see the exact values u are storing. U can see the characters because a character is stored as a byte and your editor(using which u open your file) will display it in original form only. But other data will be junks only or it wont mean what u see.

binary files are called "binary" for a reason -- they contain the computer's internal representation of the data, not something us humans can easily read. When your program reads the binary file back into the structure, then print out the values (or view them with your debugger), you should see the same thing that you saw before the structures were written out to the binary file.

binary files are called "binary" for a reason -- they contain the computer's internal representation of the data, not something us humans can easily read. When your program reads the binary file back into the structure, then print out the values (or view them with your debugger), you should see the same thing that you saw before the structures were written out to the binary file.

I know I should, but I'm not. I can understand why I wouldn't see what I expect when I open it in a text editor, but when I read it from the file it tells me the number is 0 when it's not.

Here's what I used to read it back out:

ifstream inFile("salesbin.bin", ios::binary | ios::in);
salesRecord sale;
sale.custNo = get_customer(inFile, 4);
cout << sale.custNo << endl;
inFile.close();

For reference, here is the get_customer function:

int get_customer(ifstream& inFile, int width)
{
    int i;
    char c;
	char temp[10];

	for(i=0;i<width;i++)
	{
	    inFile.get(c);
	    temp[i] = c;
	}

	return atoi(temp);
}
int get_customer(ifstream& inFile, int width)
{
    int i;
    char c;
	char temp[10];

	for(i=0;i<width;i++)
	{
	    inFile.get(c);
	    temp[i] = c;
	}

	return atoi(temp);
}

there is another concept called Endianness.
Do google about it.
Moreover, U are doing the conversion in a wrong way.
U are reading 4 bytes and not digits.


say there is a number 12 which is stored in memory as
0x0 0x0 0x0 0xC (in hex)
or it may be in the following pattern
0x0 0x0 0xC 0x0
(this depends on the endianness, big endian or little endian[READ MORE ON IT])

what u are doing is storing these in a char array. Hence the character array becomes
{0, 0, 0, [junk]} [junk] ---> whose ascii is 12.

now when u do atoi(). U will obviously get 0.

Another important thing:
How do u know that your integer data starts from the very beginning of the file ?
U need to read more on file operation ....specially binary files.

there is another concept called Endianness.
Do google about it.
Moreover, U are doing the conversion in a wrong way.
U are reading 4 bytes and not digits.


say there is a number 12 which is stored in memory as
0x0 0x0 0x0 0xC (in hex)
or it may be in the following pattern
0x0 0x0 0xC 0x0
(this depends on the endianness, big endian or little endian[READ MORE ON IT])

what u are doing is storing these in a char array. Hence the character array becomes
{0, 0, 0, [junk]} [junk] ---> whose ascii is 12.

now when u do atoi(). U will obviously get 0.

Another important thing:
How do u know that your integer data starts from the very beginning of the file ?
U need to read more on file operation ....specially binary files.

Well there must be a way that I'm not seeing, because that get_customer function was supplied with the assignment.

Well there must be a way that I'm not seeing, because that get_customer function was supplied with the assignment.

u have to use fread() to read the whole structure and not an individual member. Then u can use the particular member.

u have to use fread() to read the whole structure and not an individual member. Then u can use the particular member.

That seems to be it. I forgot to try and use the read function. I guess that happens after staring at the same thing for long periods of time.

struct salesRecord* data;
data = new salesRecord;

ifstream inFile("salesbin.bin", ios::in|ios::binary);
inFile.seekg(sizeof(salesRecord)*1); // edit to get desired record
inFile.read((char*)data, sizeof(salesRecord));
inFile.close();

cout << (*data).custNo << endl;

Works now. Thanks for the help.

That seems to be it. I forgot to try and use the read function. I guess that happens after staring at the same thing for long periods of time.

struct salesRecord* data;
data = new salesRecord;

ifstream inFile("salesbin.bin", ios::in|ios::binary);
inFile.seekg(sizeof(salesRecord)*1); // edit to get desired record
inFile.read((char*)data, sizeof(salesRecord));
inFile.close();

cout << (*data).custNo << endl;

Works now. Thanks for the help.

u r welcome.............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.