I am writing a hex dump program in C. I know there are tons of hex dump programs out there, but I wanted to write one for the experience. I have written the program in CodeBlocks, on Windows, but I can't seem to get it to work.
I am reading in a test program which is roughly 137,000 bytes, but the program stops at 417 bytes. Now, when I compile the code on Linux (as it's only a console application and is using standard C libraries), it works perfectly, and gives back the correct amount of bytes in the file. Does anyone have any idea why read() would not work on Windows, but works fine in Linux?
Below is an example of how I am reading in the file.
int main(int argc, char **argv)
{
if (argc != 2) { return 1; }
int fd = open(argv[1], O_RDONLY);
if (fd == -1) { return 1; }
unsigned char buffer[8];
unsigned int bytes = 0;
unsigned int total_bytes = 0;
while ((bytes = read(fd, buffer, sizeof(unsigned char) * 8)) > 0) {
...
total_bytes += bytes;
}
printf("Total Bytes: %d\n", total_bytes);
return 0;
}