Adak, have you taken a look at my bin file?
Adak, have you taken a look at my bin file?
No. My hex editor has taken a dump. I'll have to grab another one.
Edit:
===
Ok, I'm back with a hex editor, and all is well. Have a question for you though.
In the binary file, what's the best way to locate the mark in the file?
With just one record, I can't make a general rule.
Rest of the program is great - you'll be smiling.
Last edited by Adak; 11-22-2009 at 03:40 AM.
I'm not here to impress you, or anyone for that matter. I do however feel it necessary to point out the right way to do something. You don't teach that it's fine to modify string literals, when it is undefined behavior.
It's not my fault you use an antiquated compiler that didn't handle it right in the first place. That's like advocating fflush(stdin) just because it works on <random compiler>.
Quzah.
Hope is the first step on the road to disappointment.
Don't worry, you're not impressing me, atm.
I don't teach that it's fine to modify string literals. I've referenced Laserlight's post clarifying that point, three times now, in this thread.
How many times will it take before you read her post, and understand why I referenced it? How many more posts will it take before you quit playing your violin?
it is this way:In the binary file, what's the best way to locate the mark in the file?
you read first 3 bytes(key).
then you read N bytes until you reach \0 terminator. (Name)
after that, you count 20-N bytes(ignore them).
and then read 4 bytes (Mark)
which program do you mean? my code?Rest of the program is great - you'll be smiling.
The input portion of it.
20 - N bytes. May have to do some tweaking on that part of it.
I have to run just now, but I'll post it up when I return.
If this is a binary file with fixed lengths records, why do you keep trying to use fscanf? Why aren't you just using fread to read 27 bytes at a time, or 3 bytes, 20 bytes, 4 bytes?
Quzah.
Hope is the first step on the road to disappointment.
Because I wanted to see if you were awake, of course!
I only tested this on the one record you posted to rapidshare.Code:/* help for r00t on cprogramming forum */ #include <stdio.h> #include <string.h> #define KeyLen 4 #define NameLen 21 typedef struct { char key[4]; char name[21]; int Mark; }rec; int main(void) { size_t i, j; FILE *fp; rec recs[3]; /* The fields in the records are fixed width, (the fields are padded whenever the data doesn't fill it all the way). I've left them at their fixed widths. No subtraction to find the Mark is needed that way. */ if((fp = fopen("mu1.bin", "rb")) == NULL) { printf("\n Error opening the input file - terminating program\n"); return 1; } i = j = 0; do { j = fread(recs[i].key, KeyLen - 1, 1, fp); if(!j) break; fread(recs[i].name, NameLen, 1, fp); fread(&recs[i].Mark, 4, 1, fp); recs[i].key[KeyLen - 1] = '\0'; recs[i].name[NameLen - 1] = '\0'; printf("#%d: %s %s %d\n", i, recs[i].key, recs[i].name, recs[i].Mark); ++i; }while(1); fclose(fp); printf("\n\n\t\t\t press enter when ready"); i = getchar(); return 0; }
Last edited by Adak; 11-23-2009 at 12:21 AM.
Hey thanks a lot, Adak,
now i know how fread() works!
Thanks everyone!