[問題] 擷取檔案名稱後只想輸出前段字元

看板C_and_CPP作者 (菜邊巴)時間3年前 (2020/11/13 21:53), 編輯推噓0(008)
留言8則, 4人參與, 3年前最新討論串1/2 (看更多)
開發平台(Platform): Win10 編譯器:Dev-C++ 問題(Question): 小弟目前是C初學者,我想請教版上各位先進。 我目前想要把文件中的檔案名稱擷取前面幾個字元建立成一份清單, 但是不知道該怎麼做,目前想法是應該把字串轉為字元。 btw,檔案名稱為:20110827001040.00.led.01.00-03.00h.jpg 想擷取為20110827001040.00.led.01.00-03.00h 這是我的程式碼: /*Program*/ #include <stdio.h> #include <stdlib.h> #include <io.h> #include <string.h> int main() { FILE *fout; fout=fopen("output.txt","w+t"); if(fout==NULL){ printf("Fail to open file output!\n"); return: } struct _finddata_t fileinfo; long fHandle; int i=0; if((fHandle=_findfirst("*.jpg",&fileinfo))==-1L){ printf("no jpg\n"); return 0; } else{ do{ i++; fprintf(fout,"%s\n",fileinfo.name); }while(_findnext(fHandle,&fileinfo)==0); } _findclose(fHandle); fclose(fout); system("pause"); return 0; } 感謝QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.134.82 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1605275636.A.6DD.html

11/13 22:24, 3年前 , 1F
fileinfo.name[strlen(fileinfo.name)-4]=0;
11/13 22:24, 1F

11/13 22:24, 3年前 , 2F
顯示前補這行概念是這樣,自己完善XD
11/13 22:24, 2F

11/13 22:46, 3年前 , 3F
你有兩個.jpg在同一個檔名要怎麼處理
11/13 22:46, 3F

11/13 23:31, 3年前 , 4F
試試看 strstr(), 如果回傳結果剛好是 ".jpg" 就是找
11/13 23:31, 4F

11/13 23:31, 3年前 , 5F
到副檔名, 在結果指標以前的字元就是你想找的部分
11/13 23:31, 5F

11/14 16:55, 3年前 , 6F
3F說的是 xxxx.jpg.bak.jpg這種情況嗎?理論上附檔名
11/14 16:55, 6F

11/14 16:55, 3年前 , 7F
一定是最後一個.之後的名稱吧? XD
11/14 16:55, 7F

11/14 17:10, 3年前 , 8F
strrchr
11/14 17:10, 8F
文章代碼(AID): #1Vhe_qRT (C_and_CPP)
文章代碼(AID): #1Vhe_qRT (C_and_CPP)