|
kanmai
บุคคลไม่รู้ว่าใคร
|
 |
« ตอบ #3 เมื่อ: สิงหาคม 24, 2008, 07:01:08 AM » |
|
พี่ขอไม่คอมเม้นนะ เพราะว่าพี่ไม่ค่อยว่างเท่าไหร่ ไม่เข้าใจตรงไหนก้อเมล์มาถามนะ
#include<stdio.h> #include<stdlib.h> #include<string.h>
void saveArray(char names1[20][32],char names2[20][32],int count,char* outputFile) { int i=0; FILE* pOutfile; pOutfile = fopen(outputFile,"w"); if(pOutfile==NULL) { printf("Error: can not open output file"); exit(1); } fprintf(pOutfile,">>>List 1<<<"); for(i=0;i<count;i++) { fprintf(pOutfile,"%s\n",names1[ i ] ); } fprintf(pOutfile,"\n>>>List 2<<<"); for(i=0;i<count;i++) { fprintf(pOutfile,"%s\n",names2[ i ] ); } fclose(pOutfile); }
void sortArray(char names[20][32], int valueCount) { int i; char temp[32]; /* temp string to use for swapping */ int swapCount = 1; while (swapCount > 0) { swapCount = 0; for (i = 0; i < valueCount-1; i++) { /* if names[ i ] should be later than names [i+1] */ if (strcmp(names[ i ], names[i+1]) > 0) { strcpy(temp,names[ i ]); strcpy(names[ i ],names[i+1]); strcpy(names[i+1],temp); swapCount = swapCount + 1; } } } }
int main(int argc, char* argv[]) { int count=0; //char inputFile[128]="input1.txt"; //char outputFile[128]="output1.txt"; char* inputFile=NULL; char* outputFile=NULL; char inputBuffer[128]={'\0'}; char names1[20][32]={'\0'}; /* values to be sorted */ char names2[20][32]={'\0'}; /* values to be sorted */ FILE *pInfile = NULL; if(argc!=3) { printf("Error with input command!!!\n"); exit(1); } inputFile = strdup(argv[1]); outputFile = strdup(argv[2]); if((inputFile == NULL) || (outputFile == NULL)) { printf("Memory allocation error in strdup!!!\n"); exit(1); } pInfile = fopen(inputFile,"r"); if (pInfile == NULL) { printf("Error: can not open input file!!"); exit(1); } while(fgets(inputBuffer,128,pInfile)!=NULL) { if(strlen(inputBuffer)!=0) { sscanf(inputBuffer,"%s %s",names1[count],names2[count]); printf("%s %s\n",names1[count],names2[count]); count++; } } fclose(pInfile); sortArray(names1,count); sortArray(names2,count); saveArray(names1,names2,count,outputFile); //getchar(); }
|