I don't understand quite what you want. Do you want to incrementally process the file, reading one line from it, then abandon it and process the next? Or do you want to read the entire file into a buffer?
If you want the latter, I think this is appropriate (check for NULL return for malloc and fopen in real code for whether the file exist and whether you got enough memory).
Up vote 8 down vote favorite 4 share g+ share fb share tw.
Reallocing after every read char seems silly, reallocing after every read line doesn't seem much better. I would like to read the entire file into the array. How would you do it?
C arrays link|improve this question edited Feb 21 '10 at 22:51skaffman114k8136227 asked Jan 4 '09 at 12:57diminish82247.
I don't understand quite what you want. Do you want to incrementally process the file, reading one line from it, then abandon it and process the next? Or do you want to read the entire file into a buffer?
If you want the latter, I think this is appropriate (check for NULL return for malloc and fopen in real code for whether the file exist and whether you got enough memory): FILE *f = fopen("text. Txt", "rb"); fseek(f, 0, SEEK_END); long pos = ftell(f); fseek(f, 0, SEEK_SET); char *bytes = malloc(pos); fread(bytes, pos, 1, f); fclose(f); hexdump(bytes); // do some stuff with it free(bytes); // free allocated memory.
Yes, that would apply to my case. I meant that using realloc after each read char seems very inefficient, similarly after every read \n (to extend the array). – diminish Jan 4 '09 at 13:15 great, i'm glad it helps you – Johannes Schaub - litb Jan 4 '09 at 13:26 You should open the file in binary mode - there might be problems otherwise (check eg. Glibc manual, 12.17) – Christoph Jan 4 '09 at 13:40 oh, thanks.
I had no idea that it makes that much of a difference. – Johannes Schaub - litb Jan 4 '09 at 14:59 On POSIX systems, it shouldn't. But I'm pretty sure I once stumbled upon a bug which went away after switching to binary mode - but I can't remember what the exact issue was... – Christoph Jan 4 '09 at 15:33.
If mmap(2) is available on your system, you can open the file and map it into memory. That way, you have no memory to allocate, you even don't have to read the file, the system will do it. You can use the fseek() trick litb gave to get the size.
Void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); EDIT: You have to use lseek() to obtain the size of the file, . Int fd = open ("filename", O_RDONLY)); int nbytes = lseek(fd, 0, SEEK_END); void *content = mmap(NULL, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0).
If you want to use ISO C, use this function. It's litb's answer, wrapped with some error handling...
Best way would be to pre-allocate an amount of bytes and fread() for this amount. As long as the number of really read bytes is > 0 you can reallocate and continue reading. You will then reallocate blocks.
The block size is the important parameter for the performance of the algorithm. FILE *file = fopen("...", "r"); if (file! = NULL) { const size_t block_size = 1024; unsigned char *buffer = malloc(block_size); size_t read_bytes = 0; size_t last_read; while ((last_read = fread(buffer + read_bytes, 1, block_size, file)) > 0) { read_bytes += last_read; unsigned char *buffer2 = malloc(read_bytes + block_size); memcpy(buffer2, buffer, read_bytes); free(buffer); buffer = buffer2; } // ... do something with read_bytes of buffer free(buffer); fclose(file); }.
– diminish Jan 4 '09 at 13:38 1 It's a convenient number to reduce the number of realloc() (or, in this case, malloc()) calls. The code can be critiqued; using realloc() would be better since it might be able to extend the existing allocation instead of always allocating a new chunk. Etc.
– Jonathan Leffler Jan 4 '09 at 17:02 2 It's also generally better to not grow your buffer linearly, but exponentially (eg. Always double buffer size on overflow); this will save you realloc() calls and seems to work reasonably well in practice... – Christoph Jan 4 '09 at 17:40.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.