gpt4 book ai didi

c++ - 获取缓冲区内容,将其存储在结构中,获取新的缓冲区内容,看看结构中是否相等

转载 作者:行者123 更新时间:2023-11-30 17:14:31 25 4
gpt4 key购买 nike

我已经学习 C++ 大约几个月了......我制作了一个较小的程序来看看我在较大的程序中哪里出了问题......

我从 buffer[] 中获取信息并将其存储在结构中。当缓冲区的信息发生变化时,我想检查结构体以查看它是否与之前收到的数据不同。

我很困惑,有什么想法吗?

我可以看到数组长度是相同的,当我打印内容时,它们在控制台中看起来是相同的。我想说这是我的比较功能?或者这就是我将数据保存到结构中的方式?

提前致谢!

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

using namespace std;

struct _tag
{
char tagValue[25];
string tag;
};
_tag tag;

int isTagValueEqual(char* input, char* output){
if (input == output)
return 1;
else return 0;
}

void get_tag(char* input, char* output)
{
for (int i = 0; i < 25; i++)
{
if (input[i] == 0x20 || input[i] == ' ')
{
//output[i] = '\0';
//fill remainder of array with 0s
for (int j = i; j < 25; j++){
if(j==24)
output[j] = '\0';
else
output[j] = ' ';
}
break;
}
else
{
output[i] = input[i];
}
}
}

int main(){

char buffer[] = "248:-22:119:-23:18:-60 -71";
char raw_tagValue[25];
//initialize to 0
*tag.tagValue = 0;


get_tag(buffer, raw_tagValue);
cout << "raw_tagValue: " << raw_tagValue << endl;

//sscanf(raw_tagValue, "%[^0]", tag.tagValue); // copy char array
//strncpy(tag.tagValue, raw_tagValue, strlen(raw_tagValue));
strcpy(tag.tagValue, raw_tagValue);

//check to see if raw_tagValue is equal to itself
if (isTagValueEqual(raw_tagValue, raw_tagValue) == 1)
cout << "works" << endl << endl;
else
cout << "doesn't work" << endl << endl;

// compare raw_tagValue to tag.tagValue
if (isTagValueEqual(tag.tagValue, raw_tagValue) == 1)
cout << "works" << endl;
else{
cout << "doesn't work" << endl;
cout << "tag: " << tag.tagValue << endl << "rawTag: " << raw_tagValue << endl;
}

return 0;
}

最佳答案

一个问题:

if (input == output)

这只是比较两个指针,而不是它们指向的内容。

用途:

if (strcmp(input, output) == 0 ) 

PS您的代码中可能存在其他问题。

关于c++ - 获取缓冲区内容,将其存储在结构中,获取新的缓冲区内容,看看结构中是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30282938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com