将文本文件读入c中的数组

时间:2023-01-31 15:41:10

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?

将文本文件读入动态一维数组的最有效方法是什么?在每个读字符之后重新定位看起来很愚蠢,在每个读行之后重新定位看起来也没有那么好。我想把整个文件读入数组。你会怎么做?

3 个解决方案

#1


25  

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):

我不太明白你想要什么。您是否希望增量地处理文件,从其中读取一行,然后放弃它并处理下一行?还是要将整个文件读入缓冲区?如果你想要后者,我认为这是合适的(用真实的代码检查malloc和fopen的NULL return是否存在,是否有足够的内存):

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

#2


13  

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 .

如果您的系统上有mmap(2)可用,您可以打开文件并将其映射到内存中。这样,你就没有内存分配了,你甚至不用读取文件,系统就会这么做。您可以使用litb提供的fseek()技巧来获得大小。

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, .

编辑:您必须使用lseek()来获取文件的大小,。

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);

#3


1  

If you want to use ISO C, use this function.

如果您想使用ISO C,请使用此函数。

It's litb's answer, wrapped with some error handling...

这是litb的答案,包含了一些错误处理……

#1


25  

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):

我不太明白你想要什么。您是否希望增量地处理文件,从其中读取一行,然后放弃它并处理下一行?还是要将整个文件读入缓冲区?如果你想要后者,我认为这是合适的(用真实的代码检查malloc和fopen的NULL return是否存在,是否有足够的内存):

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

#2


13  

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 .

如果您的系统上有mmap(2)可用,您可以打开文件并将其映射到内存中。这样,你就没有内存分配了,你甚至不用读取文件,系统就会这么做。您可以使用litb提供的fseek()技巧来获得大小。

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, .

编辑:您必须使用lseek()来获取文件的大小,。

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);

#3


1  

If you want to use ISO C, use this function.

如果您想使用ISO C,请使用此函数。

It's litb's answer, wrapped with some error handling...

这是litb的答案,包含了一些错误处理……