【发布时间】:2011-11-06 22:41:18
【问题描述】:
我正在尝试使用 read 函数从文本文件中读取并存储到缓冲区。然后我必须再次检查文件以查看是否进行了任何更改,如果有,重新分配内存并将内容存储到同一个缓冲区(附加),逐个字符,直到达到 EOF。到目前为止,我的代码如下所示:
int fileSize=0;
fileSize=fileStat.st_size; /*fileSize is how many bytes the file is, when read initially*/
char buf[fileSize];
read(0, buf, fileSize);
/*now, I have to check if the file changed*/
int reader;
void *tempChar;
int reader=read(0, tempChar, 1);
while(reader!=0){
/*this means the file grew...I'm having trouble from here*/
我尝试了很多东西,但是当我尝试将“tempChar”中的内容附加到“buf”时总是遇到问题。我知道使用 realloc 功能..但我仍然遇到问题。任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
您能否粘贴您遇到问题的代码并描述您遇到的问题。此外,首先了解您如何初始化
tempChar会有所帮助。 (您粘贴的代码在调用read时使用它,根本没有初始化它。)