本帖最后由 jackz007 于 2021-3-9 10:58 编辑 #include<stdio.h>
int main(void)
{
FILE * fp ;
char ch[20] ;
if((fp = fopen("C:\\Users\\yyp\\Desktop\\t2.txt","r")) != NULL) {
fgets(ch , 10 , fp) ;
while(! feof(fp)) {
printf("%s\n" , ch) ;
fgets(ch , 10 , fp) ;
}
fclose(fp) ;
} else {
fprintf(stderr , "ERROR\n") ;
}
}
通过这个代码,你会发现,确实是每次读取了 9 个字符。
编译、运行实况D:\0002.Exercise\C>g++ -o x x.c
D:\0002.Exercise\C>x
adf212342
131aaabbb
adf212342
131aaabbb
adf212342
131aaabbb
adf212342
131aaabbb
adf212342
131aaabbb
adf212342
131aaabbb
adf212342
131aaabbb
adf212342
131aaabbb
adf212342
131aaabbb
D:\0002.Exercise\C>
如果希望照原样打印文件内容#include<stdio.h>
int main(void)
{
FILE * fp ;
char ch[20] ;
if((fp = fopen("C:\\Users\\yyp\\Desktop\\t2.txt","r")) != NULL) {
fgets(ch , 10 , fp) ;
while(! feof(fp)) {
printf("%s" , ch) ;
fgets(ch , 10 , fp) ;
}
fclose(fp) ;
printf("\n") ;
} else {
fprintf(stderr , "ERROR\n") ;
}
}
编译、运行实况D:\0002.Exercise\C>g++ -o x x.c
D:\0002.Exercise\C>x
adf212342131aaabbbadf212342131aaabbbadf212342131aaabbbadf212342131aaabbbadf21234
2131aaabbbadf212342131aaabbbadf212342131aaabbbadf212342131aaabbbadf212342131aaab
bb
D:\0002.Exercise\C>x >
|