字符串IO
- gets():读取整行输入,直至遇到换行符;丢弃换行符,存储其余字符,添加空字符,形成字符串
- puts():显示字符串,并在末尾添加换行符
- fgets():读取一行数据,直到遇到换行符,存储换行符
- fputs():输出,不加换行符
系统使用缓冲IO
- 输入-> 缓冲区-> 按下return,即加入换行符 -> 缓冲内容发送给fgets();
- fputs()-> 缓冲区 -> return -> 缓冲内容显示在屏幕上
#define MAXLEN 8
void test_gets_puts()
{
char words[MAXLEN];
puts("Enter a string:");
gets(words);
puts(words);
}
void test_fgets_fputs()
{
char words[MAXLEN];
puts("Enter another string:");
fgets(words,MAXLEN,stdin);
fputs(words,stdout);
}
void test_loop_fgets_fputs()
{
char words[10];
while(fgets(words,10,stdin)!=NULL && words[0]!='\0')
{
fputs(words,stdout);
}
}
void test_scanf_printf()
{
char name1[11], name2[11];
int count;
puts("Enter 2 names:");
count = scanf("%5s %10s", name1, name2);
printf("I read the %d names: %s %s", count, name1, name2);
}