2013-02-08 153 views
0
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

char * find_dot(); 
char * find_end(); 

int main(int argc, char * argv[]){ 

    char *file_extension[10]; 

    int i; 
    for(i = 1; i < argc; i++){ 


    //if an option 
    if(argv[i][0] == '-'){ 
     switch(argv[i][0]){ 

     default:; 
     } 


    //otherwise, should be the file 
    }else{ 
     char *dot_location_ptr; 
     char *end_location_ptr; 
     char *filename_ptr = argv[i]; 

     dot_location_ptr = find_dot(filename_ptr); 
     end_location_ptr = find_end(filename_ptr); 

     memcpy(file_extension, dot_location_ptr, end_location_ptr - dot_location_ptr); 

其中find_dot返回指向'。'的指針。在參數中,使用strrchr,find_end返回參數中'\ 0'的指針。將一個字符串從一個指針複製到另一個指針

它編譯,但我得到一個分段錯誤。我想要做的就是將文件擴展名捕獲爲字符串,並將該擴展名與其他字符串進行比較。

+0

你應該有'char',而不是一個'的char *'的數組的數組:'炭FILE_EXTENSION [10]'。請記得複製'\ 0'。 – 2013-02-08 20:39:26

+0

沒有想到的!謝謝! – user1472747 2013-02-08 20:39:51

+0

還有一件事我忘了提,看到編輯:) – 2013-02-08 20:40:10

回答

1
char *file_extension[10]; 
    ^

你不聲明file_extension權。你需要一個char數組,而不是一個指針數組。丟掉*

+0

我花了一整天關於這一點,多虧了你的鄉親,它的最後工作!謝謝!! – user1472747 2013-02-08 20:50:08