我正在努力完成一项非常简单的任务,即在打开文件后读取文件。使用 perror,我看到报告的错误是“错误地址”。这是什么意思,我该如何解决?
int freqCheck(char * fileName) {
/*Allocate buffer*/
struct stat fileStat;
stat(fileName, &fileStat);
int size = fileStat.st_size + 1;
void * buf = malloc(size);
memset(buf, 0, sizeof(buf));
if(buf = NULL) {
printf("An error has occurred while allocating memory\n");
return 0;
}/*End if*/
/*Read file*/
int fd = open(fileName, O_RDONLY);
if(fd < 0) {
perror("Open error");
return 0;
}/*End if*/
ssize_t num;
while((num = read(fd, buf, size - 1)) > 0) {
printf("num = %d\n", num);
}/*End while*/
if(num < 0) {
perror("Read error");
return 0;
} else if(num == 0) {
printf("Read worked!");
}/*End else if*/
}/*End freqCheck*/