- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在研究 C 中的输入和输出,我发现有大约十亿种不同的方式来获取输入,例如 getch、getchar、gets 和 fgets,输出也是如此(putchar、puts、 fputs 等)。
所有这些不同的 I/O 方法让我很困惑,所以我来这里是想问一下上述函数之间的基本区别是什么。
我还使用这些不同的函数编写了一些代码,并根据我所学的知识评论了我认为它们的工作方式,但我不确定我的理解是否正确。我也在其他地方阅读过它们,但是解释非常复杂而且似乎不连贯。
那么谁能告诉我我是否正确使用它们,如果不正确,我应该如何使用它们以及它们之间的主要区别是什么?
这是我的代码:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
void individualCharacters()
{
char theChar;
while ((theChar = getchar()) != '~') { // getchar() stores all characters in a line buffer as it is entered until newline is entered
putchar(theChar); // putchar() prints the characters in the line buffer and does not print a newline, line buffering depends on compiler
}
}
void withoutF()
{
char name[50];
printf("What is your name? ");
gets(name); // receives a string until newline is entered, newline is then replaced with string terminator, array limit should not be passed
puts("Hi"); // only prints one string at a time and adds the newline because gets() previously replaces the newline
puts(name);
}
void withF()
{
char name[50];
printf("What is your name? ");
fgets(name, 50, stdin); // does add a newline so the newline takes up one space in the array, it stores input until either newline is entered or array limit is reached
fputs("Hi ", stdout); // does not print a newline but prints the string input up to the array limit
fputs(name, stdout);
}
void main()
{
//sum();
//individualCharacters();
//withoutF();
//withF();
//printRandomString();
}
这些只是我编写的一些以不同方式获取输入和显示输出的函数,但我无法理解为什么有这么多不同的方式。
如果我在使用 I/O 功能时犯了任何错误,请随时告诉我,以便我进行修改。
谢谢
最佳答案
fgets
- 从指定的流中读取最多 SIZE-1
个字符到缓冲区中,如果有空间,包括尾随的换行符。在缓冲区末尾添加一个 0 终止符,使其成为有效的 C 字符串:
char buffer[SIZE];
if ( fgets( buffer, sizeof buffer, stdin ) ) // read from standard input
do_something_with( buffer );
else
error();
成功时,fgets
返回输入缓冲区的地址。在失败或文件结束时,它返回 NULL
。
fgetc
- 从指定的输入流中读取一个单个字符并返回它:
FILE *input_stream = fopen( "some_file", "r" );
int c;
while ( (c = fgetc( input_stream )) != EOF )
do_something_with( c );
gets
- 在 C99 之后被弃用,从 C2011 中完全删除。和 fgets
一样,它会从标准输入中读取一个字符序列到缓冲区中,并添加一个 0 终止符,但与 fgets
不同的是,它没有提供限制输入的机制,使得它是一种流行的恶意软件利用。此外,它不会将尾随换行符存储到缓冲区。使用它肯定会在您的代码中引入故障点。假装你从未听说过它。
getc
- 与 fgetc
相同,只是它可以作为宏实现。
getchar
- 从标准输入读取单个字符:
int c;
...
while( (c = getchar()) != EOF )
do_something_with( c );
fputs
- 将字符串写入指定的输出流:
char str[SIZE];
...
fputs( str, output_stream );
fputc
- 将单个字符写入指定的输出流:
while ( i < strsize )
fputc( str[i], output_stream );
putc
- 与 fputc
相同,只是它可以作为宏实现
putchar
- 将单个字符写入标准输出。
关于c - getchar/putchar、gets/puts 和 fgets/fputs(C 语言)之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39224580/
这个问题已经有答案了: Using printf in assembly leads to empty output when piping, but works on the terminal (2
如何仅在 putchar() 的帮助下打印整数。我想在不使用外部存储的情况下做到这一点。 这个问题在去年的一次采访中被问到。 最佳答案 在面试中遇到模糊的要求时,表达您的假设是个好主意。 我认为关于只
所以我一直在自学 C,并且遇到了来自“stdio.h”的“getchar()”和“putchar()”方法。据我了解,“getchar()”从文本流中获取最新的字符并将其存储到一个变量中,而“putc
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
有人知道 1u 在这个函数中的作用吗?以下函数接受一个整数并打印出它的位。我试图弄清楚的线是 putchar 线。我看到它需要一个无符号整数作为参数,并在二进制数大小的循环中迭代 1 或 0,这恰好发
我有以下可用的整数 putchar() 函数: void write_uint(unsigned n) { if (n / 10) write_uint(n / 10); putcha
我正在编写一些代码,在程序的某个部分得到了意外的输出,这反过来又扰乱了整个系统。 代码可以简化并缩短为: char ch; printf("Enter Number: "); while ((ch =
我的 putchar() 函数在求和后返回垃圾。 这是我的代码片段: scanf("%d", & keys); getchar(); while ((c = getchar()) != EOF) {
我正在使用“The C Programming Language”这本书来学习 C。 我完全复制了代码,但是 putchar() 不会返回用户输入的值。 #include main() {
今天我用 putchar 在 c 中写了一个代码,所以我可以将它翻译成汇编,但是我调用的最后一个 putchar 没有打印,如果我在它工作后添加一个换行符。 else if(R5 == 0x2B) /
我正在编写一些代码,在将文本进一步发送到程序之前对其进行过滤(该代码删除了除所有字母数字字符和下划线之外的所有内容),代码本身工作得很好,除了我无法找到一种方法存储它的输出以供程序的其他部分使用,如果
请看一下我的代码并告诉我为什么我的输出看起来像这样。 #include #include int main(int argc, char** argv) { char ch, source_
这是我的代码: #include #include void ignoreRestOfLine(FILE* fp) { int c; while ( (c = fgetc(fp)) !
此代码需要多个 f 并仅打印 1。有人可以向我解释一下为什么此代码不打印两个 f 吗? putchar 出现两次,但没有 EOF。我已经盯着它看了好几个小时了,但我不明白它如何只打印 1 f。 代码运
我对这种和平的代码有疑问,它应该将小写字母更改为大写,并将多个空格变为一个空格。所以它做错了什么,它以某种方式切断了第一个字母,就像当我写“abcdefg”时它给了我输出“BCDEFG”。 main(
通过 K&R,我试图了解 C。我想编写一个程序,在屏幕上打印用户的上一行,除非该字符是“a”。 int main(){ int c; while((c=getchar())!=EOF){ i
我是c编程新手,所以希望你们能帮助我解决这些问题。 1.我以为 putchar() 每次只打印 1 个字符,而当我输入几个字符(如“hello”)时,它会在允许我输入下一个输入之前打印“hello”?
我的功能: void output(int word) { file > 24) & 0xff); file > 16) & 0xff); file > 8)
在这段代码中: #include int main() { int i,p=0; while(i!=EOF) { i=getchar();
我是 C 编程的新手,想弄清楚一个看似愚蠢的疑问... 代码: #define EOF 0 main() { int c; c=getchar(); while (c!= EO
我是一名优秀的程序员,十分优秀!