0

I am writing a program for transferring files between different clients and i have a little problem.

I ran to an example where it doesn't work.

I have a pdf file and i am reading it using fread:

bytes = fread(chunk, 1, BUFLEN - 1, from_file);

After a bit of debugging i found out that this line of code does not read BUFLEN - 1 bytes (oh by the way, the file is big and i am iterating only once to take the first BUFLEN - 1 bytes, so the file has more than BUFLEN - 1 bytes). The return value of fread is actually BUFLEN - 1, but after writing this chunk to a file it has a much smaller size.

I suppose fread stops reading when running into something, but i couldn't find out what.

I opened the file with "rb" so this isn't the problem either. Moreover, my program seems to read well any text files, of any size, and this is the reason i believe it has some problems when finding different type of bytes.

EDIT: I am sending the chunk via

    send(socksv, chunk, strlen(chunk), 0);

In receive:

    n = recv(i, chunk, BUFLEN - 1, 0);

After that i write it like this

    fwrite(chunk, 1, strlen(chunk), copy_file);
Dragos
  • 776
  • 8
  • 32
  • How did you write to another file? – Rohan Apr 30 '14 at 15:43
  • Not enough information here. "I suppose fread stops reading when running into something" -- you should be able to figure out where by comparing the return value to the *expected* number of bytes read. – Jongware Apr 30 '14 at 15:43
  • why not read the whole buffer length? that raises suspicion that you could have a 1 off error, as you generally don't null terminate binary data. because there could be a zero in there anyway – Grady Player Apr 30 '14 at 15:43
  • What is the type of the `bytes` variable that you are using in order to store the return-value of `fread`? – barak manos Apr 30 '14 at 15:47
  • Let's see: 1. The problem is not in writing i already checked that out 2. The return value is exactly BUFLEN - 1, as expected, but the byte array is smaller 3. This could actually happen, i will dig it up. – Dragos Apr 30 '14 at 15:47
  • Can you edit your question and add the fwrite() statement as well? – Mahonri Moriancumer Apr 30 '14 at 15:48
  • "The byte array is smaller" - how are you checking that exactly? And how are you declaring and allocating the array? – Remy Lebeau Apr 30 '14 at 15:49
  • @user3412998 I believe that you "already checked that out", but you're mistaken about *something*, else your code would work. Could we see the `fwrite`, the `fopen` of the output file, and any other code that might modify `bytes` before writing? – Paul Roub Apr 30 '14 at 15:50

2 Answers2

2

The problem is almost certainly the use of strlen() as this will stop writing (either to the socket or to the file) when it sees a zero-byte, which there almost certainly will be inside the PDF. You need to be remembering the size of the chunk (as returned by fread()) and use this for your writes.

Also, make sure you're checking the return value of send() as it can send only part of the buffer you ask it to.

TripeHound
  • 2,721
  • 23
  • 37
0

If the file is in binary mode and fread() says it read BUFLEN-1 bytes then it really did. You say you are writing the read data to another file, so the problem has to be with your writing, not your reading. You did not show that code, though.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770