Originally Posted by
999cm999
I know I suck at C, but I'm not quite sure I see where I am doing the double read.
In this block of code:
Where is the double read coming from?
Because you call fgets() in the while condition, then fread. The fgets() happens at each iteration, which advances the file pointer. This is the same issue you had with the text file read where I thot you were using sscanf and not fscanf. But fgets is pretty useless on binary data, because it reads up until a newline. '\n' has a byte value of 10. Here's a decimal dump (using the program from post #29) of hardware.dat after I entered "toaster 10 32666":
Code:
{
1 0 0 0 116 111 97 115 116 101 | ****toaste
114 0 0 0 0 0 0 0 0 0 | r*********
0 0 0 0 10 0 0 0 -102 127 | ****/*****
0 0 | **
So: the first four bytes are the recordNum, which was 1, the bytes are in "little endian" order, ie, the least significant one first. For record #255, since the output is signed, those bytes would be "-1 0 0 0" (unsigned 255 = signed -1), for record #256, those bytes would be "0 1 0 0".
Then you have the 20 bytes of toolname, which contains "toaster". Then you have the four bytes of "quantity", and since 10 fits into one byte, that's there (notice the / on the right).
Which is where fgets would stop reading, leaving the file pointer for fread.
The last four bytes are "cost". 32666 doesn't fit into one byte, and appears as "-102 127 0 0".
So here's a textFile() that works. Compare it to what you have and consider why I changed what I changed:
Code:
void textFile( FILE *readPtr )
{
FILE *writePtr;
struct hardwareData hardware = { 0, "", 0, 0 };
if ((writePtr = fopen( "hardware.txt", "w" )) == NULL )
{
printf( "File could not be opened.\n" );
return;
}
/* copy all records from random-access file to text file */
rewind(readPtr);
while (fread( &hardware, sizeof( struct hardwareData ), 1, readPtr)) {
/* write single record to text file */
fprintf(writePtr, "%d %s %d %d\n", hardware.recordNum, hardware.toolname, hardware.quantity, hardware.cost );
fprintf(stderr, "wrote #%d, %s\n", hardware.recordNum, hardware.toolname);
}
fclose( writePtr );
writePtr = fopen( "hardware.txt", "r" );
int toolNumber;
char toolName[20];
int toolQuanity;
int toolCost;
char buf[BUFSIZ];
while ( fgets(buf, sizeof(buf), writePtr) != NULL)
{
int check;
check = sscanf( buf, "%d%[^0-9-]%d%d", &toolNumber, toolName, &toolQuanity, &toolCost );
printf( "(check = %d) %d %s %d %d\n",check, toolNumber, toolName, toolQuanity, toolCost );
}
fclose( writePtr );
}