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.