在c中扫描空格/整行

所以,我知道之前已经问过这个问题,但我似乎无法做任何事情。 我现在拥有的是:

#include struct ClothingCustomer{ char name[20]; int age; double inseam; }; struct ClothingCustomer createACustomer(){ struct ClothingCustomer aCustomer; printf("Enter Customer Name: "); scanf("%s",aCustomer.name); printf("Age: "); scanf("%d",&aCustomer.age); printf("Inseam: "); scanf("%lf",&aCustomer.inseam); return aCustomer; }; int main(){ FILE* customersFile = fopen("customers.txt","w"); for (int i = 0; i < 5; i++){ struct ClothingCustomer aCustomer = createACustomer(); fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age, aCustomer.inseam); } fclose(customersFile); return 0; } 

无论我做什么试图让它扫描不止一个单词,比如名/姓或什么的,它都有效,但这是我在运行时在控制台中获得的(使用扫描选项试图通过一个下面列出的空格;上面的代码function正常,但不允许空格):

 Enter Customer Name: Age: Inseam: Enter Customer Name: Age: Inseam: Enter Customer Name: Age: Inseam: Enter Customer Name: Age: Inseam: Enter Customer Name: Age: Inseam: 

怎么能让它不这样做? 我尝试过使用:

 [^\n] fgets(name, sizeof(name), stdin); 

每次都会发生同样的事情。

这将工作

  #include #include struct ClothingCustomer createACustomer(void); struct ClothingCustomer{ char name[20]; int age; double inseam; }; struct ClothingCustomer createACustomer(void){ struct ClothingCustomer aCustomer; { //From Here Starts The Part in Which You Are Having Problems. char c; int i; printf("Enter Customer Name: "); scanf("%s",aCustomer.name); i = strlen(aCustomer.name); // length of user input till first space do{ scanf("%c", &c); aCustomer.name[i++] = c; // reading characters after first space (including it) }while (c != '\n'); // until user hits Enter aCustomer.name[i - 1] = 0; // string terminating } printf("Age: "); scanf("%d",&aCustomer.age); printf("Inseam: "); scanf("%lf",&aCustomer.inseam); return aCustomer; }; int main(){ FILE* customersFile = fopen("customers.txt","w"); int i = 0; for (i = 0; i < 5; i++){ struct ClothingCustomer aCustomer = createACustomer(); fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age,aCustomer.inseam); } fclose(customersFile); return 0; } 

我强烈建议你看看这个答案 ,它会对你有所帮助,我在这里使用的方法在上面的答案中提到。请给出答案信用如果这个方法适合你。 以下是您遇到问题的部分的解释,现在是如何工作的。

这是怎么回事? 当用户从标准输入中输入字符时,它们将存储在字符串变量中,直到第一个空格。 之后,其余的条目将保留在输入流中,并等待下一个scanf。 接下来,我们有一个for循环,它从输入流中获取char(直到\ n)并将它们附加到字符串变量的结尾,从而形成一个与键盘用户输入相同的完整字符串。

不清楚为什么scanf(" %19[^\n], aCustomer.name)因OP而失败 。

而不是使用scanf()进行复杂输入,将用户输入与解析分开。 完全放弃使用scanf()并使用fgets()来获取用户输入。 使用sscanf()strtod()strtol()strtok()等进行解析。

务必检查用户输入的结果和解析函数的成功。

OP没有说明如何处理麻烦的输入。 在这种情况下,下面返回一个归零的ClothingCustomer。 其他错误代码或错误消息可能很有用。

 struct ClothingCustomer createACustomer(void) { // Suggest initializing struct ClothingCustomer zero = { 0 }; struct ClothingCustomer aCustomer = { 0 }; char buffer[100]; printf("Enter Customer Name: "); fflush(stdout); // insure prompt is seen before asking for input if (fgets(buffer, sizeof buffer, stdin) == NULL) return zero; buffer[strcspn(buffer, "\r\n")] = '\0'; // lop off potential line ending if (strlen(buffer) >= sizeof aCustomer.name) return zero; // too long strcpy(aCustomer.name, buffer); printf("Age: "); fflush(stdout); if (fgets(buffer, sizeof buffer, stdin) == NULL) return zero; if (sscanf(buffer, "%d", &aCustomer.age) != 1) return zero; // Let us do some range checking // https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people if (aCustomer.age < 0 || aCustomer.age > 122) return zero; printf("Inseam: "); fflush(stdout); if (fgets(buffer, sizeof buffer, stdin) == NULL) return zero; if (sscanf(buffer, "%lf", &aCustomer.inseam) != 1) return zero; return aCustomer; }