【发布时间】:2026-02-14 22:40:02
【问题描述】:
我正在尝试读取具有多个字段的用户输入。但未指定字段数。我想阅读所有的单词,直到回车。我试过这段代码,但它不起作用:
char str[256];
while(1)
{
scanf("%s", str);
if(str[strlen(str)] == '\n')
break;
else
printf("Got %s\n", str);
}
用户输入示例:
1. 存储文件1
我需要解析 store 和 file1 并跳出循环。
2. 存储文件1 文件2
我需要解析 store、file1 和 file2 并跳出循环。
想知道如何在回车符处跳出循环。
谢谢。
【问题讨论】:
标签: c loops while-loop scanf