What would be the most efficient method of reading a text file into a dynamic one-dimensional array? 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?
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
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);
}
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);
If you want to use ISO C, use this function.
It's litb's answer, wrapped with some error handling...
i need to read the table as below from file .txt and store the table in array by c and then I can print any element in the table through for loop or some conditions any help
0 1 2 3 4 5
0 -- 1 2 1 1 1
1 0 -- 2 3 3 5
2 0 1 -- 3 3 3
3 1 1 2 -- 4 1
4 3 3 3 3 -- 5
5 1 1 1 1 4 --