-1

I'm working in C right now and I'm reading a disk image that uses the FAT12 system. In the command line, I accept the name of a file to copy from the disk image to the local folder. Here's my question, when I'm traversing through all the bytes that I need to fetch for a particular file that I will be copying to the local folder, what function(s) should use to write those bytes to my local file?

By the way, the files that need to be copied from the disk may be PDF, .txt, etc.

As of now, I use the below to open (create) the file locally and then the function below it to write bytes to that file (this function is where I think the problem lies) There's a lot more code that actually traverses through the bytes in the FAT12, but I simply need to know what I'm doing wrong during the write:

FILE *file = fopen(arg[2],"wb"); //To create the file on the local folder
fprintf(file,"%#04x ",buffer[index]); //Write a byte from the file that is being copied to the local file from the disk image

My code works perfectly in regards to getting the appropriate bytes from the files that I am copying, but for some reason, the .PDF files do not open, and the .txt files only display the copied bytes (no text).

Any advice would be greatly appreciated!

Vimzy
  • 1,871
  • 8
  • 30
  • 56
  • Provide [MCVE](http://stackoverflow.com/help/mcve). We cannot tell what is wrong without seeing how you create the data in `buffer`. – MikeCAT Nov 23 '15 at 00:49
  • 2
    Or maybe you mean just copy the bytes as bytes? say, `putc(buffer[index], file);` – MikeCAT Nov 23 '15 at 00:49
  • @MikeCAT THANKS MAN! that did the trick! now the PDF file actually copy over as a pdf, and the .txt files copy over as actual .txt files. So what was the difference here? how did putc inject the byte into the file differently? was the way I was doing it a string insertion into the file? – Vimzy Nov 23 '15 at 00:57

1 Answers1

0

A big shoutout to MikeCAT for this! So when you're trying to copy a file from a disk image to the local folder, whenever you insert a specific byte of the file you're copying into the file that will be receiving the copy, use this:

putc(buffer[index], file);

NOT THIS:

fprintf(file,"%#04x ",buffer[index]);

The above will copy the byte into the file as a string.

Vimzy
  • 1,871
  • 8
  • 30
  • 56