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' 的指针。

它编译,但我得到一个分段错误。我要做的就是将文件扩展名捕获为字符串,并将该扩展名与其他字符串进行比较。

4

1 回答 1

1
char *file_extension[10];
     ^

你说的file_extension不对。您需要一个 char 数组,而不是指针数组。放下*.

于 2013-02-08T20:40:09.180 回答