矢量打印出错误的值

时间:2011-10-12 07:39:20

标签: c++ vector

您好我对以下代码提出了疑问。我将一个char字符串复制到一个矢量中,然后在屏幕上打印出这个矢量。程序将字符串复制到文本文件中,但向量仅给出向量的最后一个元素。我错在哪里? :

#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;
    }
}

2 个答案:

答案 0 :(得分:1)

不是一个真正的答案,但我的意见是你应该真正学习如何阅读xml文件。你的代码是如此复杂,虽然它真正做的只是尝试“破解解析”一个xml文件,许多人会觉得不值得尝试纠正它。

答案 1 :(得分:0)

您的问题始于代码顶部

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];

此点buffer为空,因此length()返回0并且您只为C字符串分配一个字节。