我在阅读空间的阅读输入方面有问题
我制作了此功能以获取输入:
void entrada_dados(Time* time, int i){
scanf("%s %d %d", time[i].nome, &time[i].gols_marcados, &time[i].gols_sofridos);
};
输入是以下形式:
2
Campinense
23
12
ABC
30
13
主要是:
int main(void) {
int n = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++){
entrada_dados(time, i);
}
....
我的问题是团队名称有一些空间喜欢“圣保罗”。我尝试了一些表格来解决,但是没有人解决我的问题。
我尝试了:
void entrada_dados(Time* time, int i){
fscanf(stdin, "%[^\n] %d %d", time[i].nome, &time[i].gols_marcados, &time[i].gols_sofridos);
};
和:
void entrada_dados(Time* time, int i){
fgets(time[i].nome, 100, stdin);
scanf("%d", &time[i].gols_marcados);
scanf("%d", &time[i].gols_sofridos);
}
但是在第一种情况下,输出一无所有,第二种情况下输出会错过某些情况。有人可以帮助我理解这个问题吗?
编辑1: .NAME的定义是:
typedef struct Time{
char nome[100];
int gols_marcados;
int gols_sofridos;
} Time;
编辑2:
解决方案: 解决它的一种方法:
Try two fscanfs fscanf(stdin, " %[^\n]", time[i].nome);
fscanf(stdin, "%d %d", &time[i].gols_marcados, &time[i].gols_sofridos);
谢谢你们。
I made this function to get input:
void entrada_dados(Time* time, int i){
scanf("%s %d %d", time[i].nome, &time[i].gols_marcados, &time[i].gols_sofridos);
};
The input is in this form:
2
Campinense
23
12
ABC
30
13
The main is:
int main(void) {
int n = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++){
entrada_dados(time, i);
}
....
My problem is when the team name have some space like to "São Paulo". I have tried some forms to solve, but no one solved my problem.
I tried:
void entrada_dados(Time* time, int i){
fscanf(stdin, "%[^\n] %d %d", time[i].nome, &time[i].gols_marcados, &time[i].gols_sofridos);
};
and:
void entrada_dados(Time* time, int i){
fgets(time[i].nome, 100, stdin);
scanf("%d", &time[i].gols_marcados);
scanf("%d", &time[i].gols_sofridos);
}
but in the first case the output have nothing, and second case the output miss some cases. Someone can help me to understand this problem?
Edit 1:
The definition of .name is:
typedef struct Time{
char nome[100];
int gols_marcados;
int gols_sofridos;
} Time;
Edit 2:
Solution:
One way to solve it:
Try two fscanfs fscanf(stdin, " %[^\n]", time[i].nome);
fscanf(stdin, "%d %d", &time[i].gols_marcados, &time[i].gols_sofridos);
Thank you guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您必须处理带有空格的字符串,所以最好将
fgets
为它们使用。但是混合
fgets
和scanf
效果不佳。我们可以用scanf
fgets
替换为sscanf
。要解码号码,我们可以使用
strtol
或sscanf
我们利用
time> time
出现在a 上的每个元素/成员的事实输入文件中的单独行,因此我们可以为每行进行fgets
。这简化了代码并使错误检查更加容易。这是重构代码。它是注释的。
我没有这样做,但是,如果这些序列完成了很多,我们可以将其中一些序列组合在辅助函数中以减少一些代码复制(例如,结合了
fgets
的功能,然后是该代码复制。sscanf
)这是程序输入:
这是程序输出:
Because you have to handle strings with spaces, it's better to use
fgets
for those.But mixing
fgets
andscanf
doesn't work too well. We can replacescanf
withfgets
followed bysscanf
.To decode numbers, we can use
strtol
orsscanf
We take advantage of the fact that each element/member of
Time
appears on a separate line in the input file, so we can dofgets
for every line. This simplifies the code and makes error checking easier.Here is the refactored code. It is annotated.
I didn't do this, but, if these sequences are done a lot, we can combine some of these sequences in helper functions to reduce some code replication (e.g. a function that combines the
fgets
followed by thesscanf
)Here is the program input:
Here is the program output: