C - 3rd scanf modifies a variable from 2nd scanf
我想我已经尝试了任何方法(刷新标准输入,使用scanf消耗换行符等),但是没有任何工作如我所希望的那样。 由于某些原因,第三个scanf在以下代码中修改了第二个scanf中的变量:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <stdio.h> int main() { char first_name[16], last_name[21]; char filename[11]; FILE *opening; printf("The program saves your first and last name into a file.\ "); printf("Enter your first name:"); scanf("%s", first_name); getchar(); printf("Enter your last name:"); scanf(" %s", last_name); getchar(); printf("File where you want to save your name:"); scanf(" %s", filename); opening = fopen(filename,"wb"); fprintf(opening,"%s %s", first_name, last_name); printf("\ Successfully saved the data!"); fclose(opening); return 0; } |
输出:
1 2 3 4 5 6 | The program saves your first and last name into a file. Enter your first name: John Enter your last name: Doe File where you want to save your name: filename.txt Successfully saved the data! |
一切正常,但文件名.txt的内容是这样的:
John t
我猜想't'字符以某种方式来自'txt',但是我刚开始学习C语言,我不知道如何解决这段代码。 上师可以帮我吗?
您的
您编写的
1 | char filename[20]; |
它应该工作。
尽管使用
在字符串上使用
如果扫描字符串,则应始终通过将此数字添加到传递给
1 2 3 4 5 6 7 8 | char file_name[11]; ... scanf("%10s", file_name); /* As file_name provides memor for 11 characters, read a maximum of 10 characters into file_name leaving 1 character room for the necessary `0-`terminator indicating the end of the"string". */ |
此外,您的代码还会错过
最好做这样的事情:
1 2 3 4 5 6 |
如果要输入filename.txt作为文件名,那么您将超出
要解决此问题,请将
否则,一切看起来都很好。