我目前正在研究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功能犯了任何错误,请随时告诉我,以便我做出修改。
谢谢
答案 0 :(得分:2)
getch()
是一个提示按键的功能,其中不会回显该字符。
相反,getche()
将回显角色。
gets()
会读取stdin
中的字符,但不安全(请改用fgets()
)。
getchar()
将返回它从stdin
读取的下一个字符,与调用getc()
stdin
作为参数的字符相同。
fgets()
从流中读取字符(如文件或stdin
),并将它们作为C字符串存储到str
中,直到读取(num-1)个字符或换行符或达到EOF
。
putch()
将字符写入stdout
。它与以putc()
作为参数调用stdout
相同。
puts()
将str
指向的C字符串写入stdout
并附加换行符。它开始复制,直到它到达空终止符(\0
)。 Null-terminator未打印。
fputs()
将str
指向的C字符串写入流,基本上类似于puts()
至于您的代码,请尽量避免使用gets()
,因为它不安全且使用scanf()
,puts()
也是如此。请改用printf()
。
在这里,请阅读:http://www.cplusplus.com/reference/cstdio/
我知道它是C ++参考,但库函数是相同的
答案 1 :(得分:1)
fgets
char * fgets ( char * str, int num, FILE * stream );
Get string from stream
从流中读取字符并将它们作为C字符串存储到str中,直到读取了(num-1)个字符或者到达了换行符或文件结尾,以先到者为准。 换行符使fgets停止读取,但它被认为是有效字符,因此它包含在复制到str的字符串中。 在读取字符后,空字符会自动附加在str中,以表示C字符串的结尾。
fputs
int fputs ( const char * str, FILE * stream );
Write string to stream
将str指向的字符串写入流。 该函数从指定的地址(str)开始复制,直到到达终止空字符('\ 0')。此最终空字符不会复制到流中。
getchar
function
<cstdio>
int getchar ( void );
从stdin获取角色。 返回标准输入(stdin)中的下一个字符。 它等同于以stdin作为参数的getc。
putchar
function
<cstdio>
int putchar ( int character );
将字符写入stdout 将字符写入标准输出(stdout)中的当前位置,并将内部文件位置指示器前进到下一个位置。 它等同于putc(character,stdout)。
gets
:从标准输入到内存
puts
:从记忆到标准输入
Example :
#include<stdio.h>
void main( )
{
char name[10];
printf("What is your first and last name?");
gets(name);
puts(name);
}
答案 2 :(得分:1)
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
- 将单个字符写入标准输出。