gpt4 book ai didi

c - 在c中处理多行字符串

转载 作者:行者123 更新时间:2023-11-30 14:48:15 24 4
gpt4 key购买 nike

我想将这些作为输入

3
abc def
deg fgh
ghdfete fdgtr dhjgg

输出应该是

abc def
deg fgh
ghdfete fdgtr dhjgg

我写的代码

#include <stdio.h>
int main(){
int t;
scanf("%d",&t);
char a[100];
while(scanf("%[^\n]%*c",a) == 1){
printf("%s\n",a);
--t;
if(t == 0)
break;
}
return 0;
}

没有打印任何东西。请帮忙。

最佳答案

考虑对所有输入使用fgets

#include <stdio.h>
int main(){
char value[50] = "";
int t;
int result = 0;
char a[100];

do {
printf ( "enter an integer\n");
if ( fgets ( value, sizeof value, stdin)) {
if ( 1 != ( result = sscanf ( value, "%d", &t))) {
if ( EOF == result) {
fprintf ( stderr, "found EOF\n");
return 0;
}
}
}
else {
fprintf ( stderr, "problem fgets\n");
return 0;
}
} while ( result != 1);

printf ( "enter text\n");
while( fgets ( a, sizeof a, stdin)) {
printf("%s\n",a);
--t;
if(t == 0)
break;
}
return 0;
}

关于c - 在c中处理多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com