close
標題:

aa.jpg

 

此文章來自奇摩知識+如有不便請留言告知

C wirte a text file

發問:

FILE*fp1; int num,i,c; if(!(fp1=fopen("file.txt","r"))) { printf("Error opening file.txt for writing."); return 1; }---------------------------------------------------------------------------------------如果個result 係 :ID Product... 顯示更多 FILE*fp1; int num,i,c; if(!(fp1=fopen("file.txt","r"))) { printf("Error opening file.txt for writing."); return 1; } --------------------------------------------------------------------------------------- 如果個result 係 : ID Product Quantity 01 ABC 2 ------------------------------------------------------------------------------------------ 應該點寫落去?? 更新: 我想將 ID Product Quantity 01 ABC 2 寫入去file.txt到 更新 2: sorry...我講得唔清楚 果d 01 ABC 2 <---user輸入的

最佳解答:

你想點??請講清楚!! 2010-05-11 13:01:26 補充: FILE*fp1; int num,i,c; if(!(fp1=fopen("file.txt","w"))) { printf("Error opening file.txt for writing."); return 1; } fprintf(fp1,"ID Product Quantity "); fprintf(fp1,"01 ABC 2 "); fclose(fp1); 2010-05-11 13:58:40 補充: 那user需要輸入什麼,何時停止?輸入/輸出範例? 2010-05-12 14:13:36 補充: void orders() { int i,num; char input[81]; FILE *file; if(!(file = fopen("file1.txt","w"))) { printf("Error"); exit(100); } printf(" How many orders : "); scanf("%d",&num); for(i=0;i< num;i++) { printf("Product %d :",i+1); fgets(input,sizeof(input),stdin); fputs(input,file); } fclose(file); } 2010-05-12 14:14:06 補充: char * Search(char *search) { static char input[81]; FILE *file; if(!(file = fopen("file1.txt","r"))) { printf("Error"); exit(100); } while (fgets(input, sizeof(input),file)) { if (strstr(input,search)) { fclose(file); return input; } } fclose(file); return 0; } 2010-05-12 14:14:34 補充: void main() { char input[80], *res; orders(); while (1) { printf("input search string:"); fgets(input,sizeof(input),stdin); if (input[0]==' ') break; input[strlen(input)-1]=0; if (res = Search(input)) printf("found: %s ",res); else printf("not found! "); } } 2010-05-12 23:09:08 補充: add fflush(stdin); after scanf("%d",&num); like this scanf("%d",&num); fflush(stdin);

其他解答:

user 要輸入 id ,product name, quantity. 另外,user search his order status by id or product name.!
arrow
arrow

    oob73zj92b 發表在 痞客邦 留言(0) 人氣()