我想创建一个程序,它接收用户的一些输入单词,将输入存储在数据结构中,然后将数据结构打印到txt文件中。
现在,我只构建了它接收一些输入并将它们存储在数据结构中的部分,然后我添加了一个printf来打印数据结构,只是为了测试。
问题是,如果我在char "one“上输入"test_one",它将打印"test_one”,但如果我输入"test one",它将只打印"test“。我如何编辑它,使其存储整个单词,而不是只存储一个单词?我能用指针做到这一点吗?
#include <stdio.h>
struct inputs {
char one[30];
char two[30];
char three[30];
};
int main(void)
{
struct inputs inputs = {"", "", ""};
scanf("%s%s%s", inputs.one, inputs.two, inputs.three);
printf("\n%s;%s;%s\n", inputs.one, inputs.two, inputs.three);
}
} 发布于 2018-08-25 04:54:00
使用标准"%s“的scanf将只读取输入,直到它到达空格。试着这样读,直到有新的一行:
scanf("%[^\n]s", inputs.one);
scanf("%[^\n]s", inputs.two);
scanf("%[^\n]s", inputs.three);https://stackoverflow.com/questions/52011388
复制相似问题