0

I'm trying to read the contents of a file with fread, however the contents are not being written to the ptr. I've verified with the ftell() function that the position in the stream is changing, however no content is written to the ptr.

I've implemented the same functionality in another code, the only difference being that in that case it was a struct instead of char *, I can't figure out based on the documentation why this is the case.

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
    fprintf(stderr, "Usage: recover infile\n");
    return 1;
}

char *infile = argv[1];

// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

char *block;
block = malloc(sizeof(char)*64);
fread(block, 1, 64, inptr);
printf(" es  %s \n", block);

int position = ftell(inptr);
printf("position %d \n", position);

free(block);
fclose(inptr);

}

The malloc is currently a placeholder btw.

Any explanations as to why the data on the file isn't being written to block?

bard91
  • 1
  • You mean the printf doesn't show anything? It should though... your infile is ok? – Déjà vu Nov 23 '17 at 00:34
  • correct printf doesn't show anything (other than "es " obvs), the infile definitively has content, I don't know if there's any other factor that could affect it given that fopen works – bard91 Nov 23 '17 at 00:38
  • I don't see any problem with this program. Either malloc returns NULL or there are some weird characters (eg 0 or a BOM) at the top of your file – Déjà vu Nov 23 '17 at 00:47
  • 2
    Works here. (BTW: are you reading a binary file(starting with a NUL) BTW2: your buffer is not NUL-terminated. – wildplasser Nov 23 '17 at 00:49
  • You may want to adjust your `fopen` to specify a mode of `"rb"` – David Hoelzer Nov 23 '17 at 00:58
  • I hadn't thought about that, the file is raw data and it does have a ton of 00s at the beginning which I forgot would be read as ASCII. I tried reading all of it by changing tit to real all the file, but I'm still seeing an empty prin block = malloc(sizeof(inptr)); fread(block, 1, sizeof(inptr), inptr); – bard91 Nov 23 '17 at 00:59
  • It is indeed a binary file, I tried some other files and it does work, I did change it to fopen to rb, but it is still not working, by the buffer are refering to block? I'm not sure how I would make it Nul-terminated and what difference that would make. – bard91 Nov 23 '17 at 01:07
  • Hmm - not sure whether you got it. If your input file starts e.g. with `"\x00\x00\x00\x01\x0b\xad\xf0\x0d"` the `fread()` will read this all. If you `printf("%s", block);` it sees the 0-termination in first char of `block` and hence outputs an empty string. (The debugger can show you the real contents of `block`.) In other cases, the read `block` may contain only non-0 bytes. In this case, `printf()` will not stop at block end -> UB. To come around the latter issue, make `block` one `char` larger as you intend to read and store a 0 in the last element of it -> UB fixed. – Scheff's Cat Nov 23 '17 at 07:25
  • To see _any_ contents read in the `block`, you may output it as hex dump. You can "borrow" a hex dump function from one of the plenty sample codes found here and at other sites e.g. this: [SO: how to get hexdump of a structure data](https://stackoverflow.com/a/7776146/7478597). – Scheff's Cat Nov 23 '17 at 07:33

1 Answers1

0

In general, if you want to understand better what is the output of a printf, you could write:

printf("Test: <%s> - (len=%lu)\n", block, strlen(block));
Fabio_MO
  • 713
  • 1
  • 13
  • 26