i read data in from a file and store it in a buffer
now, im trying to extract data from that buffer and store it in another variable. here is my code:
Code: Select all
class Element
{
public:
int GetNodeSize() {return nodesize;}
char * GetNodeElement() {return node;}
void SetNodeSize(int size) {
nodesize = size;
node = new char [nodesize];
}
void SetNodeElement(char *temp) {
node = temp;
}
private:
int nodesize;
char *node;
};
Code: Select all
void parseBuffer(char *buffer, long lSize) {
int x, y, loop;
char *tempnode;
for (x = 0; x <= lSize; x++)
if (buffer[x] == '<') {
y=x;
while (buffer[y] != '>')
y++;
XML[0].SetNodeSize((y-x)-1);
tempnode = new char [XML[0].GetNodeSize()];
for (loop = 0; loop < XML[0].GetNodeSize(); loop++)
tempnode[loop] = buffer[(x+1)+loop];
XML[0].SetNodeElement(tempnode);
delete [] tempnode;
break;
}
printf("%s\n", XML[0].GetNodeElement());
}
however, the printf statement returns garbage.
if i change the printf statement to print tempnode instead (and comment out "delete tempode"), it comes close... it prints out the string i want, plus a few trailing garbge characters.
i figure it has something to do with the way i pass it to SetNodeElement (or maybe the assignment), but ive been trying all sorts of things and cant figure it out. what am i doing wrong?