【问题标题】:strcat() deleting strings when trying to create a space-separated string. (C programming)strcat() 在尝试创建以空格分隔的字符串时删除字符串。 (C 编程)
【发布时间】:2011-10-08 22:39:53
【问题描述】:

过去几个小时我一直在努力解决这个问题,这是我在 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”之间的两个空格。我的程序仍然正确地添加了空格,即使由于某种原因一个单词被删除了。我已经尝试过许多不同的输入,它始终是输入中的第二个字符串被删除。有谁知道怎么回事?

附:使用 gcc -g -O0 -Wall -Wextra -std=gnu99 -lm 编译

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

【问题讨论】:

  • 仅供参考,strcat 在几乎所有情况下都被认为是不安全的。使用strlcatsnprintf 作为替代品(strncat 不太适合替代品)。

标签: c string concatenation strcat


【解决方案1】:

您感兴趣的代码以:

开头
            char* debugString = optarg;

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

要解决此问题,请尝试:

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

这将为您分配 1000 个字节来使用 strcat 填充字符串位。 有责任确保您不会写超过debugString 缓冲区的末尾(因此,如果您发现要连接的字节数超过 1000 个,则必须决定您想要什么与此有关)。

【讨论】:

  • 我向你的高手低头。 鞠躬。那解决了它。谢谢。是时候重新学习指针了。
猜你喜欢
  • 1970-01-01
  • 2021-07-17
  • 2021-05-06
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多