[英]how can I use scanf() to read a file including space in C
我不允许调用除scanf
、 printf
和feof
之外的 C 库中的任何其他printf
。 当我尝试使用下面的代码时,它只读取最多空格字符的文件。 有没有办法在不使用"%[^\\n]"
情况下读取整行? 我的最终目标是使用feof
文件读取整个文件,但首先,我需要知道如何使用scanf
读取一行。 是否可以?
#include <stdio.h>
int main(){
char ar[50];
scanf("%s", ar);
printf("%s", ar);
}
一次读取一个字符。
#include <stdio.h>
int main(void) {
char ch;
while (scanf("%c", &ch) == 1) {
printf("%c", ch);
}
}
PS 永远不要对生产代码这样做,也不要在简历上承认你这样做了。
遵循@zwol 的建议。
“我的最终目标是使用 feof file 读取整个文件” -->为什么“while (!feof (file))”总是错误的? .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.