0

I can seem to only read file into memory if I explicitly declare the buffer size. This works

#include <stdio.h>
int main(){
  FILE *fp = fopen("test.log", "rb");
  char buffer[37];
  fread(buffer, 1, 36, fp);
  printf("%s", buffer);
}

This will add junk to the output

#include <stdio.h>
int main(){
  FILE *fp = fopen("test.log", "rb");
  fseek(fp, 0, SEEK_END);
  long siz = ftell(fp);
  rewind(fp);
  char buffer[siz + 1];
  fread(buffer, 1, siz, fp);
  printf("%s", buffer);
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Zombo
  • 1
  • 62
  • 391
  • 407
  • 2
    Conceptually wrong. In C, any declaration of array should indicate the size at COMPILE time. If you want to create an array dynamically, use `malloc` – SwiftMango Aug 04 '12 at 17:43
  • You open the file in binary mode, but print it as a string. This only works if the data is really text. Also, you do not terminate the buffer. Remember that when creating an array it's not automatically zeroed out. – Some programmer dude Aug 04 '12 at 17:47
  • Maybe irrelevant as it is not assured what the data is actually, you are not `NUL` terminating your string. Try adding `\0` to the end of char array & see what it prints out – another.anon.coward Aug 04 '12 at 17:50
  • I . . . have absolutely no idea how that second one compiled. – geometrian Aug 04 '12 at 17:51
  • 3
    @IanMallett Why not? Variable length arrays are standard well over 10 years. – Daniel Fischer Aug 04 '12 at 17:51
  • 3
    @texasbruce, IanMallett Please see [Variable Length Arrays (VLA)](http://en.wikipedia.org/wiki/Variable-length_array) – Marlon Aug 04 '12 at 17:52
  • 4
    @texasbruce While declaring a VLA the size of a file is questionable, since the file can easily be too large for your stack, I see no problems with VLAs in principle, they're mighty fine when you know the size is not too large. – Daniel Fischer Aug 04 '12 at 17:54
  • @StackUnderflow Thanks for the info. Didn't know the standard updated.. – SwiftMango Aug 04 '12 at 18:46

2 Answers2

7

insert buffer[siz]='\0'; before printf("%s", buffer);

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

Try a different approach - use a "memory map". What it does is it allows you to access the file as if it was a memory block. This can dramatically improve performance while simplifying your code at the same time.

Read more about it at http://en.wikipedia.org/wiki/Mmap

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
  • This doesn't answer the question asked, and there's no indication that a memory-mapped file is needed here. The question specifically asked how to "read a file into memory", which is not what your answer does. There's a difference between "reading a file into memory and "treating a file like it's in memory"." – Ken White Aug 04 '12 at 17:53