我正在尝试打开并读取txt文件,它应该显示其中的内容,但它只显示1个字符作为输出“ÿ”。
以下是源代码:
int main(){
FILE *p;
p=fopen("D:\\eclipse_workspace_cpp\\PL_ODEV2\\inputbookdb.txt","r");
char c;
do{
c = fgetc(p);
printf("%c",c);
}
while(c != EOF);
fclose(p);
return 0;
}
答案 0 :(得分:1)
您可以通过char读取文件char:
int main(void) {
FILE *p;
// Open file
if ((p = fopen("D:\\eclipse_workspace_cpp\\PL_ODEV2\\inputbookdb.txt","r")) == NULL) {
// Couldn't open file
return EXIT_FAILURE;
}
// Step through the file until EOF occurs
int c;
while ((c = fgetc(p)) != EOF) {
printf("%c", c);
// You might use putchar(c); instead, take a look at the comment below
}
// Close file
fclose(file);
return EXIT_SUCCESS;
}