加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用scanf读取多行输入

发布时间:2020-12-16 10:23:44 所属栏目:百科 来源:网络整理
导读:为类编写程序,仅限于scanf方法.程序接收可以接收任意数量的行作为输入.使用scanf接收多行输入时出现问题. #include stdio.hint main(){ char s[100]; while(scanf("%[^n]",s)==1){ printf("%s",s); } return 0;} 输入示例: Here is a line.Here is another
为类编写程序,仅限于scanf方法.程序接收可以接收任意数量的行作为输入.使用scanf接收多行输入时出现问题.

#include <stdio.h>
int main(){
    char s[100];
    while(scanf("%[^n]",s)==1){
        printf("%s",s);
    }
    return 0;
}

输入示例:

Here is a line.
Here is another line.

这是当前的输出:

Here is a line.

我希望我的输出与我的输入相同.使用scanf.

解决方法

尝试此代码并使用Tab键作为分隔符

#include <stdio.h>
int main(){
    char s[100];
    scanf("%[^t]",s);
    printf("%s",s);

    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读