2011-10-12 107 views
-3

嗨我有一個關於下面的代碼的問題。我將複製一個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; 
    } 
} 
+0

你能減少代碼到問題的本質嗎? –

+0

請提供一些輸入xml文件。 – arne

+7

調試它。找出它開始出錯的地方。然後只發布相關的代碼。 –

回答

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字符串分配只是一個字節。

1

不是一個真正的答案,但我的意見是,你應該真的學會如何閱讀一個XML文件。你的代碼太複雜了,儘管它真的只是試圖「破解」一個xml文件,很多人會覺得它不值得去糾正它。