【发布时间】:2016-06-28 11:26:13
【问题描述】:
就像我在标题中所说,我不知道如何在 C 中打印 .txt 文件的所有内容。 这是我做的一个不完整的功能:
void
print_from_file(items_t *ptr,char filemane[25]){
char *string_temp;
FILE *fptr;
fptr=fopen(filemane, "r");
if(fptr){
while(!feof(fptr)){
string_temp=malloc(sizeof(char*));
fscanf(fptr,"\n %[a-z | A-Z | 0-9/,.€#*]",string_temp);
printf("%s\n",string_temp);
string_temp=NULL;
}
}
fclose(fptr);
}
我很确定 fscanf 中存在错误,因为有时它不会退出循环。
谁能更正一下?
【问题讨论】:
-
你的
string_temp=malloc(sizeof(char*));你正在为你的string_temp分配一个char指针大小的内存。 malloc 本身返回一个指针。