Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
对于一个类项目,我必须读取一个文件并将其内容存储在要排序的数组中。一件好事是文件将通过标准输入管道输入,但唯一的问题是我不能假设文件没有增长:我必须检查 EOF。如何使用 malloc() 和 realloc() 执行此操作?
这是一种方法:使用 malloc 分配一个具有合理默认大小(如 4096)的字符数组。开始将标准输入中的字符读取到数组中,直到获得 EOF。每当数组填满时,调用realloc以使数组大小加倍。
realloc