從檔案中讀取擷取字元並對擷取的字元進行排序
阿新 • • 發佈:2018-11-22
#include "stdio.h" #include "stdlib.h" int main(void) { char pathname[50], c_read[5]; int arr[200]={0}; char char_read; FILE *fp; printf("Input the file path:"); scanf("%s", pathname);//獲取檔案位置 fp=fopen(pathname, "r");//開啟檔案 if(fp){//success open file int a=0; while(!feof(fp))//一直讀到結束退出 { char_read=fgetc(fp);//讀取一個字元 if(char_read=='$'){ for(int i=0; i<4; ++i){//處理獲取需要的字元 char_read=fgetc(fp); if(char_read=='-'){ arr[a]=atoi(c_read);//把字元轉成整數 ++a; break; } sprintf(c_read+i, "%c", char_read); } } } } /* * *pai xu da dao xiao */ int tmp; for(int i=0; i<200-1; ++i){//排序 for(int j=0; j<200-i-1; ++j){ if(arr[j]==0) continue; if(arr[j]<arr[j+1]){ tmp=arr[j+1]; arr[j+1]=arr[j]; arr[j]=tmp; } } } for(int n=0; n<200; ++n){ printf("%d\n", arr[n]);//打印出結果 } fclose(fp); return 0; }
讀取的檔案格式
$28-5bef5a00$$29-5bef5a00$$01-5bef5a00$$33-5bef5a00$$66-5bef5a00$