33

I have an assignment for coding a Huffman algorithm. I have the whole problem organized in my head, but I'm having some trouble with file handling.

The problem is: the algorithm is supposed to compress ANY kind of file.

My solution: read the file as a byte array, then with an int array[256]={0} for each byte, get it's int n corresponding value and increment the array[n]. If I didn't make it clear, let me know.

So, I've done lots of researching, but don't understand how to get bytes from ANY kind of file and how to handle them.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
alissonlacerda
  • 351
  • 1
  • 3
  • 6
  • I saw many topics with this subject, but non of them were clear to me. I you say so, please link one here. Thanks :) – alissonlacerda Feb 27 '14 at 04:47
  • I see couple of problems... first I would load your file into a `char array[]`. Than what's wrong with a plain `fopen()`/`fread()` preventing it to open **any** kind of file?. Finally, please make a try and report what's wrong with it. – Sigi Feb 27 '14 at 04:47
  • Question edited. Removed the sentence asking to recommend some off-site resource. – alissonlacerda Feb 27 '14 at 13:07
  • Problem solved. Thanks user1274193. Now I can open any kind of file and store it's bytes in an array. – alissonlacerda Feb 27 '14 at 13:09

2 Answers2

71
FILE *fileptr;
char *buffer;
long filelen;

fileptr = fopen("myfile.txt", "rb");  // Open the file in binary mode
fseek(fileptr, 0, SEEK_END);          // Jump to the end of the file
filelen = ftell(fileptr);             // Get the current byte offset in the file
rewind(fileptr);                      // Jump back to the beginning of the file

buffer = (char *)malloc(filelen * sizeof(char)); // Enough memory for the file
fread(buffer, filelen, 1, fileptr); // Read in the entire file
fclose(fileptr); // Close the file

Now you have an array of bytes containing the file's contents.

NateS
  • 5,751
  • 4
  • 49
  • 59
user1274193
  • 1,104
  • 1
  • 10
  • 11
  • 1
    Thank you so much! I'm getting close to what I want. I'll study more and do some tests. – alissonlacerda Feb 27 '14 at 05:37
  • shouldn't you call "buffer[filelen] = '\0';" after fclose? – mexok Feb 01 '15 at 10:20
  • This gives me a segmentation fault at fseek() – Singh Oct 11 '15 at 21:42
  • 2
    @Commander3000 If the whole array is to be treated as a string then NULL termination makes sense but the OP asked about reading in a binary file ie "ANY" kind of file. – Harry Feb 19 '16 at 06:50
  • 2
    I just noticed the answer above is also assuming NULL termination by adding an extra byte in the call to malloc, it shouldn't. If the binary file has N bytes it does not need (N + 1) bytes to be malloc'd for the buffer. – Harry Feb 19 '16 at 07:23
  • 1
    This has no error handling, it doesn't check that the entire file has been read. And why is the buffer's type char? – Aleksandr Dubinsky Jul 13 '21 at 11:09
-2

How about trying binary file IO:

  FILE *f=fopen("example.bin","rb");
  char c;
  //loop for each byte to the end
  {
    size_t fread(&c, (size_t)1, (size_t) 1, f);
    array[c]++;
  }

Or something along the lines!!

Suryavanshi
  • 595
  • 1
  • 7
  • 24
  • If I'm trying to accomplish reading a file into a `byte[]` and this answer is marked down without comment, I've learned nothing. – IAbstract Nov 21 '21 at 19:34
  • @IAbstract Reading into an array with pointer arithmetic is frowned upon because is memory unsafe and is general bad coding practice. See the accepted answer for an example of proper heap-based buffer allocation. – belkarx Feb 22 '22 at 00:30