【发布时间】:2015-05-17 03:02:38
【问题描述】:
我已经学习 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] = '\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;
}
【问题讨论】:
标签: c++ c arrays struct comparison