私は何かを試したと思います(標準入力のフラッシュ、改行を消費するためのscanfなど)が、期待どおりに何も機能しません。次のコードでは、何らかの理由で、3 番目の scanf が 2 番目の scanf の変数を変更します。
#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.\n");
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("\nSuccessfully saved the data!");
fclose(opening);
return 0;
}
出力:
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!
filename.txt の内容が次のとおりであることを除いて、すべて問題なくダンディです。
ジョン・t
「t」文字は何らかの形で「txt」から来ていると推測していますが、C の学習を始めたばかりで、このコードを修正する方法がわかりません。教祖が私を助けてくれませんか?