获取缓冲区内容,将其存储在结构中,获取新的缓冲区内容查看结构中是否相等
get buffer contents, store it in struct, get new buffer contents see if equal in struct
我已经学习 c++ 大约几个月了...我制作了一个较小的程序以查看在较大的程序中哪里出了问题....
我正在从缓冲区[] 中获取信息并将其存储在结构中。当缓冲区的信息发生变化时,我想检查结构以查看它是否与之前收到的数据不同。
我很困惑,有什么想法吗?
我可以看到数组长度相同,当我打印内容时,它们在控制台中看起来是相同的。我想说这是我的比较功能?或者这就是我将数据保存到结构中的方式?
提前致谢!
#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] = '[=10=]';
//fill remainder of array with 0s
for (int j = i; j < 25; j++){
if(j==24)
output[j] = '[=10=]';
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++ 大约几个月了...我制作了一个较小的程序以查看在较大的程序中哪里出了问题....
我正在从缓冲区[] 中获取信息并将其存储在结构中。当缓冲区的信息发生变化时,我想检查结构以查看它是否与之前收到的数据不同。
我很困惑,有什么想法吗?
我可以看到数组长度相同,当我打印内容时,它们在控制台中看起来是相同的。我想说这是我的比较功能?或者这就是我将数据保存到结构中的方式?
提前致谢!
#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] = '[=10=]';
//fill remainder of array with 0s
for (int j = i; j < 25; j++){
if(j==24)
output[j] = '[=10=]';
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您的代码中可能还有其他问题。