I am trying to read a file byte by byte (this is important because I have to measure performance). I can't seem to get the fread to work properly. Right now it just gives me the last byte of the file.
This is what I have:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *fileptr;
char *buffer;
long filelen;
int i;
fileptr = fopen(argv[1], "rb");
fseek(fileptr, 0, SEEK_END);
filelen = ftell(fileptr);
rewind(fileptr);
buffer = (char *)malloc((filelen+1)*sizeof(char));
for(i = 0; i < filelen; i++) {
fread(*&buffer, 1, 1, fileptr);
}
printf("File len: %ld\n", filelen);
printf("%s\n",*&buffer);
fclose(fileptr); // Close the file
return 0;
}
Any help is appreciated
*&buffer
is actually the same asbuffer
or&buffer[0]
. This is because&
takes the address of the variablebuffer
, creating a pointer of typechar**
, then*
dereferences that pointer, turning it back into the samechar*
that we started from, akabuffer
.getc
instead offread
.fread
is defined to work as if by repeated calls togetc
orfgetc
anyway.*&buffer
was wrong, I just said it was about as meaningful as*&*&*&*&*&*&*&buffer
ofbuffer+1-1
. :)