1

在过去的几个小时里,我一直在努力解决这个问题,这是我在 3 年学习编程中遇到的奇怪问题之一。

我试图通过连接在标准输入的 argv 中找到的字符串来创建一个更长的、以空格分隔的字符串。这是我最初写的代码:

#include <stdio.h>
#include <string.h>
#include <getopt.h>

#include "stringtable.h"
#include "strhash.h"
#include "auxlib.h"


int main (int argc, char **argv) {
    int c;
    char* filename;

    while((c = getopt(argc, argv, "@:Dly")) != -1){
        switch(c){
            case '@': 
                printf("@ detected\n");
                char* debugString = optarg;
                int pos_in_input = optind;
                while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
                    strcat(debugString, " ");
                    strcat(debugString, argv[pos_in_input]);
                    pos_in_input++;
                    if(argv[pos_in_input] == NULL){break;}                  
                }
                printf("%s\n", debugString);
                break;
            case 'D':
                printf("D detected\n");
                break;
            case 'l':
                printf("l detected\n");
                break;
            case 'y':
                printf("y detected\n");
                break;
            default :
                printf("default detected\n");
        }
    }
}

以上大部分是我未完成的选项处理。感兴趣的代码段位于 case : "@" 下的 switch 语句中。while 循环应该通过连接在 argv 中找到的字符串来创建 debugString,当字符串以“-”开头或到达 argv 结尾时停止。我还使用“strcat(debugString,”“);”在字符串之间添加空格 添加空格给我带来了麻烦。

如果我以这种方式连接所有字符串而不添加空格:

            while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
                strcat(debugString, argv[pos_in_input]);
                pos_in_input++;
                if(argv[pos_in_input] == NULL){break;}                  
            }

我得到以下信息:

 Input: -@what is going on here?
Output: @ detected
        whatisgoingonhere?

这就是我期望它的工作方式。但是,如果我运行添加空格的代码:

            while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
                strcat(debugString, " ");
                strcat(debugString, argv[pos_in_input]);
                pos_in_input++;
                if(argv[pos_in_input] == NULL){break;}                  
            }

然后我得到以下输出:

 Input: -@what is going on here?
Output: @ detected
        what  going on here?

注意“what”和“going”之间的两个空格。我的程序仍然正确地添加了空格,即使由于某种原因一个单词被删除了。我已经尝试过许多不同的输入,它始终是输入中的第二个字符串被删除。有谁知道发生了什么?

PS 用 gcc -g -O0 -Wall -Wextra -std=gnu99 -lm 编译

我创建的所有包含文件都没有在这段代码中使用,所以问题不在我的包含中。

4

1 回答 1

1

您感兴趣的代码以:

            char* debugString = optarg;

这使得debugString指针指向指向的同一个地方optarg。这不会以任何方式分配“新”字符串。通过调用strcat(debugString, ...),您将覆盖在结束后出现在内存中的任何其他内容optarg。混乱将接踵而至。

要解决此问题,请尝试:

            char debugString[1000];
            strcpy(debugString, optarg);

This will allocate 1000 bytes for you to stuff bits of string into using strcat. It is your responsibility to ensure that you do not write past the end of the debugString buffer (so if you find you have more than 1000 bytes to concatenate, you'll have to decide what you want to do with that).

于 2011-10-08T22:42:33.437 回答