我正在编写一个程序,我需要用户像这样输入一行(1, Stelios, 1.81, Greece)。
我想到的第一件事就是使用gets(),因为我想接受空格输入。在几次尝试编译都没有成功之后,我读到由于危险,它被从C-11中删除了。
接下来,我使用了scanf()和fgets(),但它跳过了输入,就像它们从来没有出现过一样,似乎什么都不起作用。
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *f;
char line[30];
char name[30];
printf("\nGive the name: \n");
scanf("%s", name);
f = fopen(name, "w");
printf("\nGive the line to enter file: ");
//gets(line);
//fgets(line, 50, stdin);
//scanf ("%[^\n]", line);
//scanf ("%[^\n]%*c", line);
printf("%s", line);
fclose(f);
}这就是我试过的一切。
发布于 2019-11-09 06:01:56
尝试将getchar();放在scanf("%s", name);之后,然后使用get ()获得您想要的行
https://stackoverflow.com/questions/58772474
复制相似问题