扫描到新行 [英] Scanning until new line

查看:41
本文介绍了扫描到新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读所有输入的文本,直到输入新的行字符.

I want to read all the text entered until a new line character is entered.

这是我的代码.

int i=0;
char ch[MAX];
printf("Enter the text\n");
while(true)
{
     scanf("%c",&ch[i]);
     if(ch[i]=='\n')
         break;
     i++;
}

但是当我尝试执行时,它只读取一个字.

But when I try to execute it reads only one word.

我也试过 scanf("%s",ch); 但结果是一样的.

I have also tried scanf("%s",ch); but the result is the same.

推荐答案

在依赖 ch[i] 之前,您没有检查 scanf() 是否成功一个有效的值,这不是一个好主意.

You're not checking that scanf() succeeds before relying on ch[i] to have a valid value, that's not a good idea.

只需使用 fgets() 即可阅读全文立即排队.

Just use fgets() to read a whole line at once.

这篇关于扫描到新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆