【发布时间】:2014-03-06 07:05:16
【问题描述】:
在用户输入的命令行中:
cmatch -i -n hello test1.in test2.in
“hello”是程序将在输入文件中搜索匹配行的字符串。
现在我被困在 printf 这个字符串上。我是 C 新手,不知道字符串数组是如何工作的。
int main(int argc, char **argv){
options opts;
program_name = basename (argv[0]);
scan_options (argc, argv, &opts);
int arrLength=argc-optind;
char *strings[arrLength];
char *fileNames[arrLength-1];
const int fileNamesLength=arrLength-1;
int argi; int i=0;
for (argi = optind; argi < argc; ++argi) {
printf ("operand[%d] = \"%s\"\n", argi, argv[argi]); //printing operands
strings[i]=argv[argi]; //assigning strings
i++;
}
char *stringToMatch=strings[0];
printf("String to match=%s\n",stringToMatch);
int f;
for(f=0; f<fileNamesLength; f++){
fileNames[f]=strings[f+1];
printf(fileNames[f]);
}
printf("GET THROUGH");
现在的输出是:
操作数[3] = "你好"
操作数[4] = "test1.in"
操作数[5] = "test2.in"
要匹配的字符串 = "hello"
0 [main] cmatch 6092 open_stackdumpfile: Dumping stack trace to cmatch.exe.stackdump
那么 *stringToMatch 有什么问题?
【问题讨论】:
-
optind设置为什么?我没有看到它在任何地方定义 -
最后一个-option之后第一个操作数的位置。它是我的老师提供的..
-
你用的是什么编译器?
-
我不确定....我在 Eclipse 中编写代码并使用 gcc Run->cmd 编译文件
-
那么你的编译器就是 GCC。我正在使用 Visual Studio 2013 并测试了您的代码。我必须将
3的常量值分配给optind并稍微修改代码以解决缺少的功能和不同的语义,但是当我运行它时,我无法重现您的错误。您可以提供更多信息吗?喜欢实际的堆栈跟踪、调用堆栈或更详细的错误信息?