从文件读取到矢量C ++

时间:2014-09-24 17:26:33

标签: c++ vector ifstream

我尝试读取文件并从文件中获取一些信息,然后从文本文件中为每个元素提供输出。

目前我遇到代码将构建并运行的问题,但在尝试打开文件时程序崩溃,IE'没有响应'。

首先,这是我的文件数据

0 0 0 285 10 0x00 Z INDEX
5 0 0 000 00 0x00 A NameA
1 2 0 10 100 0x21 B NameB
1 2 1 35 100 0x22 C NameC
2 1 1 55 100 0x23 D NameD
3 0 1 10 10 0x51 E NameE
3 2 1 35 10 0x57 F NameF
4 2 1 55 10 0x41 G NameG

这是我在收集数据的程序中的功能。

#define WINVER 0x0500

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <fstream>
#include <vector>
#include <sstream>

using namespace std;
vector<vector<int> > AIntVector;
vector<int> BIntVector;
vector<vector<string> > AStringVector;
vector<string> BStringVector;

void ReadFile()
{
    // Vector is cleared as the working file maybe changed 
    //without restarting the program. Data is stored into 
    //2 vectors depending on the information.

    AIntVector.clear();
    BIntVector.clear();
    AStringVector.clear();
    BStringVector.clear();
    string FileName;
    cout << "Please enter the working file name." << endl;
    cin >> FileName;
    if(FileName=="quit")
    {
        exit(0);
    }
    ifstream WorkingFile;
    system("CLS");
    WorkingFile.open(FileName.c_str());

    string tkey, pkey, name;
    int stata, statb, statc, coordx, coordy, key;

    while(WorkingFile >> stata >> statb >> statc >> coordx 
                >> coordy >> tkey >> pkey >> name)
    {
        stringstream ss;
        ss << hex << tkey;
        ss >> key;
        BIntVector.push_back(stata);
        BIntVector.push_back(statb);
        BIntVector.push_back(statc);
        BIntVector.push_back(coordx);
        BIntVector.push_back(coordy);
        BIntVector.push_back(key);

        AIntVector.push_back(BIntVector);

        BIntVector.clear();

        BStringVector.push_back(pkey);
        BStringVector.push_back(name);

        AStringVector.push_back(BStringVector);

        BStringVector.clear();
    }
}

感谢您的任何反馈!

0 个答案:

没有答案