2013-02-08 149 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