[問題] C程式的問題

看板C_and_CPP作者 (阿詠~^^萬歲)時間14年前 (2009/09/30 22:07), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
題目如下: Write a program to read a series of names, one per line, from standard input ,and write out those names spelled in reverse order to a file called file1. Write another program to read the names in from file1, reverse them again, sort them, and write them out to a file called file2. 我第一部分快做完了 但是他會重複寫入我打的名字 不知道怎麼修改 程式碼如下: #include <stdio.h> #include <string.h> int main(void) { FILE *file; int i=0; int j=0; int k=0; char filename[20]; char name[20]; char name2[20]; printf("Enter the name of the file: "); gets(filename); file=fopen(filename,"w"); if(file!=NULL){ printf("Enter several names (Enter EOF to end input):\n"); while(!feof(stdin)){ gets(name); for(i=(strlen(name)-1);i>=0;i--){ name2[j++]=name[i]; } name2[j]='\0'; fprintf(file,"%s\n",name2); for(k=0;k<19;k++){ name[k]=0; } } } fclose(file); return 0; } 我想不出來原因.. 請大家幫幫我 還有跟大家說一下對不起 我不太會用PTT發文 所以打的不好請勿見怪 謝謝各位.. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.130.138.118

09/30 22:12, , 1F
gets(name); 後面加個j=0;
09/30 22:12, 1F

09/30 22:13, , 2F
可以了耶..感謝!!
09/30 22:13, 2F
文章代碼(AID): #1AmsOcPK (C_and_CPP)