开发者

Vector print out wrong value

Hi I got a question about the following code. I will copy a char string, which i read out of a file into a vector and print out this vector on the screen. the program copy the string out ouf the textfile but the vector only gives out the last element of the vector.开发者_开发技巧 what did i wrong here?? :

#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <vector>
#include "mbusread.h"
using namespace std;


int main()
{

    int dev_nr;
    int anzahl;
    ifstream addr;
    string buffer;
    string dev_no;
    stringstream devss;
    char *reg_no_c = new char [buffer.length()+1];
    char *start_reg_c = new char [buffer.length()+1];
    char *adresse_c = new char [buffer.length()+1];
    char *id_c = new char [buffer.length()+1];
    char *zeit_c = new char [buffer.length()+1];

    addr.open("Addr.xml", ios::in); //Addressendatei auslesen
    do //Anzahl suchen
    {
        getline(addr, buffer);
    }   while(strcmp (buffer.c_str(), "<Anzahl>")!=0);
        getline(addr, buffer);

        stringstream bufferss(buffer);
        bufferss >> anzahl;
        anzahl++;

        vector<const char *> ip(anzahl);
        vector<const char *> start_reg(anzahl);
        vector<const char *> reg_no(anzahl);
        vector<const char *> id(anzahl);
        vector<const char *> zeit(anzahl);


    for(dev_nr=1;dev_nr<anzahl;dev_nr++)
    {
        addr.seekg(ios::beg);
        devss << "<dev" << dev_nr << ">";
        dev_no = devss.str();
        devss.str("");
        devss.clear();

        do {
            getline(addr, buffer);
            if(addr.eof())
            {
             break;
            }
          } while (strcmp (buffer.c_str(),dev_no.c_str()) != 0);

        do // ip suchen
        {
            getline(addr, buffer);
        }while(strcmp (buffer.c_str(), "<ip>")!=0);
        getline(addr, buffer);
        if(addr.eof())
        {
         break;
        }
        strcpy(adresse_c, buffer.c_str()); // ip einlesen
        ip[dev_nr - 1] = adresse_c;
        cout << ip[dev_nr - 1] << endl;

        do // start_reg suchen
        {
            getline(addr, buffer);
        }while(strcmp(buffer.c_str(), "<start_reg>")!=0);
        if(addr.eof())
        {
         break;
        }
        getline(addr, buffer);              //Schreibt Startregister in Daten
        strcpy(start_reg_c, buffer.c_str()); // ip einlesen
        start_reg[dev_nr - 1] = start_reg_c;
        cout << start_reg[dev_nr - 1] << endl;

        do // reg_no suchen
        {
            getline(addr, buffer);
        }while(strcmp(buffer.c_str(), "<reg_no>")!=0);
        if(addr.eof())
        {
         break;
        }
        getline(addr, buffer);              //Schreibt die Anzahl der ausgelesenen Register in Daten
        strcpy(reg_no_c, buffer.c_str()); // ip einlesen
        reg_no[dev_nr - 1] = reg_no_c;
        cout << reg_no[dev_nr - 1] << endl;

        do // ID suchen
        {
            getline(addr, buffer);
        }while(strcmp(buffer.c_str(), "<ID>")!=0);
        if(addr.eof())
        {
         break;
        }
        getline(addr, buffer);              //Schreibt die ID des Sensors in Daten
        strcpy(id_c, buffer.c_str()); // ip einlesen
        id[dev_nr - 1] = id_c;
        cout << id[dev_nr - 1] << endl;

        do
        {
            getline(addr, buffer);
        }while(strcmp(buffer.c_str(), "<Time>")!=0);
        if(addr.eof())
        {
            break;
        }
        getline(addr, buffer);
        strcpy(zeit_c, buffer.c_str()); // ip einlesen
        zeit[dev_nr - 1] = zeit_c;
        cout << zeit[dev_nr - 1] << endl;

    }

    for(int i = 0; i < anzahl; i++)
    {
        cout << "ip " << ip[i] << endl;
        cout << "id " << id[i] << endl;
        cout << "start_reg " << start_reg[i] << endl;
        cout << "reg_no " << reg_no[i] << endl;
        cout << "zeit " << zeit[i] << endl;
    }
}


Not a real answer, but my opinion is that you should really learn how to read a xml file. Your code is so convoluted, although what it really does is simply try to "hack an parse" a xml file, that many people will feel like it is not worth trying to correct it.

  • Xerces is the most famous C++ XML Api, though is is a huge library. See a sample with xerces DOM reading here : http://xerces.apache.org/xerces-c/program-dom-2.html

  • If xerces is a too big library, why not try TinyXML : see a sample here : http://www.grinninglizard.com/tinyxmldocs/tutorial0.html


Your problem starts right at the top of the code

string buffer;
string dev_no;
stringstream devss;
char *reg_no_c = new char [buffer.length()+1];
char *start_reg_c = new char [buffer.length()+1];
char *adresse_c = new char [buffer.length()+1];
char *id_c = new char [buffer.length()+1];
char *zeit_c = new char [buffer.length()+1];

A this point buffer is empty, so length() returns 0 and you allocate just a single byte for the C strings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜