【问题标题】:How to edit an element in a streaming char array如何编辑流式字符数组中的元素
【发布时间】:2014-11-18 14:17:18
【问题描述】:

我有来自 fgets 的记录(使用 popen() 的网络数据,而不是文件),它们是 const char * 数组格式。

const char * cstr = buff;

第三项是一个字符串,需要删除或更改为零。 如何以 5 为增量访问 const char * 数组流中的第三个元素?

1
2
string
4
5
1
2
string
4
5

代码:

while(fgets(buff, sizeof buff, fp) != NULL)
{
        const char * cstr2 = buff;
        for(int i = 0; i < 5; ++i){
                if(i == 3){
                        if (!strcmp(cstr2, "Buy\n")) {
                                printf("Found One!\n");
                                cstr2[2] = 0;
                        }
                        if (!strcmp(cstr2, "Sell\n")) {
                                printf("Found Two!\n");
                                cstr2[2] = 0;
                        }
                }
        }
}

预期输出:

1
2
0
4
5
1
2
0
4
5

错误: 没有匹配和: 错误:分配只读位置 '*(cstr2 + 2u)'

如何正确访问 char 流式 char 数组中的第三个元素?

【问题讨论】:

  • 如果到处都是strcmp 调用,这不完全是 C++ 代码。
  • 您的代码看起来像 C,而不是 C++。如果您正在使用/编译 C++,则应该使用 std::string 而不是 C 字符串。
  • 您将面临的一个问题是const char * cstr2const,因此您不能这样做:cstr2[2] = 0;
  • 我没有看到您的代码正在使用过滤输入。这些知识对于回答您的问题至关重要。第一个问题(编译)是由 cstr2 声明中的 'const' 限定符引起的。更严重的问题是你想替换第三个字符??? (cstr2[2]) 的当前输入行,而不是您指定的内容。
  • 不是第三个字符,而是第三个项目。共有 5 项。

标签: c++ arrays


【解决方案1】:

此解决方案之前由匿名发布:

char* getmyData()
{
    char buff[BUFSIZ];
    FILE *fp = popen("php getMyorders.php 155", "r");
    if (fp == NULL) perror ("Error opening file");
    size_t size = 1000;
    char* data = (char*)malloc(size);
    char* ptr = data;
    std::string::size_type sz;
    int i=0;
    while(fgets(buff, sizeof(buff), fp) != NULL)
    {
            const char * cstr2 = buff;
            const char* test = ptr;
            //for(int i = 0; i < 5; ++i)
            {
                    if(i == 2){
                            if (!strcmp(cstr2, "Buy\n")) {
                                    printf("Found One!\n");
                                    strcpy(ptr,"0\n");
                                    //ptr+=2;
                            }
                            else
                            if (!strcmp(cstr2, "Sell\n")) {
                                    printf("Found Two!\n");
                                    strcpy(ptr,"0\n");
                                    //ptr+=2;
                            }
                            else
                            {
                                strcpy(ptr,cstr2);
                                ptr+=strlen(cstr2);
                            }
                    }
                    else
                    {
                        strcpy(ptr,cstr2);
                        ptr+=strlen(cstr2);
                    }

                    try
                    {
                        int nc = std::stod (test,&sz);
                        std::cout << "Test: " << 1+nc << "\n";
                    }
                    catch(...)
                    {
                    }

                    i++;
                    if (i==5)
                        i=0;


            }

            if (ptr-data+100>=size)
            {
                int ofs = ptr-data;
                size*=2;
                data = (char*)realloc(data,size);
                ptr = data+ofs;
            }
        }
    return data; // DONT FORGET TO call free() on it
}

【讨论】:

    【解决方案2】:

    从您的示例代码中不清楚预期的输出是什么。它是一个整数数组吗?格式化的文本字符串?一个字节数组?我们不知道。

    假设您有一个文本格式的输入并且想要一个文本格式的输出,一个简单的解决方案是写入一个具有正确值的新字符串,而不是尝试修改您的输入缓冲区。

    如果您知道输入记录的确切格式,您可以使用fscanf 进行解析,而不是手动进行。您可以使用ssprintf 来格式化输出字符串。

    正如其他人指出的那样,如果您可以使用 C++,您将拥有更安全/更轻松的选择。请评论您是否愿意使用 C++。

    【讨论】:

    • 数据以文本/字符串的形式出现,但转换为 int 以进行数学运算。见上面的 cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2022-06-10
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多