- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
#include<stdio.h>
int main(void) {
FILE *fp;
int ch;
fp = fopen("input.txt", "w");
printf("Enter data");
while ((ch = getchar()) != EOF) {
putc(ch, fp);
}
fclose(fp);
fp = fopen("input.txt", "w");
while ((ch = getc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
}
第一个 while
循环如何停止用户的输入?因为存在 EOF
作为条件。
否则我是否需要使用
for 循环?
最佳答案
EOF
是一个值,不是一个函数。它本质上被定义为一个宏。
供引用,来自C11
, 第 7.21.1 章,<stdio.h>
EOF
which expands to an integer constant expression, with typeint
and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;[...]
以防万一, getchar()
失败,它将返回一个定义为 EOF
的值.
引自手册页(强调我的)
fgetc()
,getc()
andgetchar()
return the character read as an unsigned char cast to an int or EOF on end of file or error.
EOF
表示可能不适合 char
的值类型。您必须使用 int
输入 ch
变量。
How does the first while loop stop inputting from user?
在 Linux 上使用 CTRL+D,在 Windows 上使用 CTRL+Z。
关于c - EOF 宏如何与 getchar 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34816138/
我很抱歉我英语不好。 让我们看看下面的代码。 main = getChar 首先,main会被求值,它的值是“getChar”,但是编译器不知道“getChar”的值,所以编译器会求值“getChar
我正在尝试比较用户插入的两个(或更多)输入。我正在为它使用 getchar() 函数,但它似乎不起作用......我需要先检查它的字母(英文),以及是否连续没有 2 个(或更多)大写或小写字母。这是我
当我想弄清楚 getchar() 到底做了什么时,这一小段 loop 确实让我感到困惑。 int i; int c; for (i = 0; i 验证您的输入。 因此,您应该在完成 getchar 后
#include int main(){ char ch; while((ch=getchar())!=EOF){ putchar(ch); } cha
我在关注《C Primer Plus》这本书,遇到这样一段代码: // echo_eof.c -- repeats input to end of file. #include int main(v
我只是编程的初学者。我正在学习 K&R 的书《C 编程语言》。我一边看一边对这个问题越来越好奇——当有一个循环从输入中一个一个地获取字符时,我在循环中放置了一个输出函数,我认为其结果就像在输入每个字符
人们通常会提到 while( getchar() != '\n' ) 当谈到清除输入缓冲区或溢出时。 fgetc 或 getc 也可以。例如: while( fgetc(stdin) != '\n'
一旦遇到'~',之后的任何内容都不会被打印出来,控制就出来了while循环 while((c = getchar()) != '~') { putchar(c); printf(" "
我有一个执行大量处理的小程序。您可以通过按回车键打印进度。 我实现它的方法是在主线程中完成处理,同时我有一个 pthread 不断循环 getchar() 以等待输入键。 问题是当我完成处理时。发生这
我试图解决 K&R 中的问题 1.6。问题是—— 验证表达式getchar() != EOF 为 0 或 1。 我找到了一个可行的解决方案: #include int main(){ int
我正在尝试开发一个(简单的)个人项目,它使用经典的“MOD 10”规则充当信用卡验证器。 我在项目的第一部分遇到了一些困难。 目标是这样的: 从用户那里获取 16 个字符,一次一个。 每输入一个字符,
对于我的一个练习,我们需要逐行阅读并仅使用 getchar 和 printf 输出。我正在关注 K&R,其中一个示例显示了使用 getchar 和 putchar。根据我的阅读,getchar() 一
我只是想知道如何 getchar() 实现了吗?是不是像下面这样?以这种方式读取单字节效率非常低。它是否使用一些缓冲? 伪代码: int getchar() { char buf[1];
在 K&R 书中的以下代码示例中,如果我将 putchar(c) 替换为 printf("%c", c),代码的工作方式相同。但是,如果我用 printf("%d", c) 替换它,它会给出乱码输出。
我想选择性地中止 getChar行动。我需要以下功能: getChar' :: (Char -> IO ()) -> IO (IO ()) 如果是abort IO ()) -> IO (IO ())
所以我知道 getchar() 在输入结束或发生错误时返回 EOF。我也知道我可以通过 ferror(stdin) 和 feof(stdin) 来检查哪些情况发生了。我想知道特别是在什么情况下会发生错
我无法忽略空格/制表符。当用户从命令提示符输入字符时,它应该只接受除空格或制表符空格之外的任何字符。 例如: $ ./a.out Character: = 那么,就是= 如果 $ ./a.out Ch
我正在使用 chrome devtools network API和下面的代码,但总是在我打开 devtool 窗口 (F12) 时启动警报窗口。有什么方法可以在不打开该窗口的情况下启动警报,例如,当
所以我知道 getchar() 在输入结束或发生错误时返回 EOF。我也知道我可以通过 ferror(stdin) 和 feof(stdin) 来检查哪些情况发生了。我想知道特别是在什么情况下会发生错
所以我一直在自学 C,并且遇到了来自“stdio.h”的“getchar()”和“putchar()”方法。据我了解,“getchar()”从文本流中获取最新的字符并将其存储到一个变量中,而“putc
我是一名优秀的程序员,十分优秀!