代码上没问题检查一下D:\sample.txt是否存在里面是不是有内容可以加一个判断在fopen后。 if(fp == NULL){ printf("open file failed\n"); return -1;}