我想寫一本線(包括空格)的文件中的一個去與下面的代碼: - 上面代碼的意外的行爲,並得到()
//in main
char ch[40];
FILE *p;
char choice;
p=fopen("textfile.txt","w");
printf("%s\n","you are going to write in the first file");
while (1)
{
gets(ch);// if i use scanf() here the result is same,i.e,frustrating
fputs(ch,p);
printf("%s\n","do you want to write more");
choice=getche();
if (choice=='n'|| choice=='N')
{
break;
}
}
結果是令人沮喪的我,很難解釋,但我仍然會嘗試。 如果我進入,比如說,
"my name is bayant."
並按進入statment說到屏幕
"do you want to write more"
是好到現在,但是當我prees的關鍵除了「n」或「N '(所要求的程序來寫多行的邏輯),則該消息
"do you want to write more"
打印again.Now如果我按比其他鍵‘n’或‘N’上但屏幕程序的同一行的打印跟隨並打印聲明
"do you want to write more"
4倍,這是詞的數量,即4在此case.By下面這個呆板過程我得到想要的行上我的文件,但如果響應於聲明的第一次印刷
"do you want to write more"
我按「n」或「N」,那麼只有第一個單詞,即「我的」在這種情況下打印在文件上。 那麼解決方案是一次性在文件上寫出完整的一行?爲什麼在這種情況下get()和fputs()似乎無效? thanxxx提前。
希望得到()'因爲C11 – Manu343726
得到()緩衝區溢出天堂!!否則請不要使用gets,反而使用fgets。在Windows使用gets_s() – tesseract
@tesseract我知道它,但即使我使用scanf()問題仍然存在。 – YakRangi