从计算机内存中读取时出现的问题,以前从文件中获取的信息

时间:2011-02-28 11:56:36

标签: c++

我遇到的问题是我写了一个必须从计算机内存中获取信息的代码。此信息以前来自“.txt”文件,并以“模拟数据库”的特定方式存储在计算机内存中,但是当访问信息时我遇到了问题,因为代码没有以我想要的方式运行。看看代码,你就会明白我的意思。

我附上了一个我使用的“.txt”文件的例子和我写的代码。

Table.txt

1|1|30
0|20713TRVl\|8304pCXx|10600UeVt|30935giNrIs|4295D_GvCZM|9311aXuA|30063iTE`G|22658p``PwVJ|18362ZcCE|22644Y]OH|17221UG[]]nX|17084xvEcJVA|3657NP\sg|28737qMZNFm`|895Xupv|16773wASvjpe|5681zqniXpS|10873dwRLmCi|22173KSc`|18129Ekxa|16735rsqT`h|15514pCIB_oE|24295hFk_h|7752\PZX|2233wWOO|29169Y]sFDGx|669[u\M|11533EU]L|25080jaLU
1|19710WM``|11558iiGJg|16155ieHOV|28466QLoVB|18135ygKI|17776VmzB_tj|6481iUwYmN|17666_RDKvt|31467^LIMj|22316YWzc|12797YgoH|6353awg]|27166\FdVg|20077BrEzm|5447qeLC_EH|9193hhQ[l_B|17844WZW`fMe|29811STcAQpO|28993\pLiCh|4987VRSZ]q|12728yCSy]w[|13678ZRPx|5236yMl_|27648Fo_PTXb|6822iznH|13276jQCw|20282daHHmrb|26134eDWqr]u|12310eKt[NZC
2|27807qgNgei|11243Jr_QNtb|24086MzGN]f|9987tugrZj^|9760XEtQVF|28587yY\SH|4842RX`d|3041J]xQgm|24409adRD[U|18525]`nO|24535HyalLG|18439Ilqd|23040tqvfj|8036ROxWCT|25261h[O_|25226eSfjQJ|21524mSJYF|2528[ztVA|31877OznR|27727x_ck_|24510VFKV^xQ|30384MPPOTG|13334nF^xvTM|19217dOsn|2222RQCxC|23913F[vkSsJ|23733uzQQsJc|29996_MMfiRl|10372dYZSwjD
3|31258oSce|7118Vzz\|19941YMRaGx[|18653sMCN|1849klofOhd|14780zuoLZk|19426Ptacg|6885KL^]E|16484Sv]F|29183NEIv|26521aSoDDXU|5456iJqSU|30851qgpz]Tr|22097plFFPFi|12998mL^jt|3993wejdD|8889QUiKc|12706oZd_DXD|3512^EBc]|5691[xXE|14064IfJa|20823jGOxpQ|15712EfWHA|15008cPHve\|15683wrVb|5808IMQcBG|25292xVmOVDp|31242eiTvZF|30793qWgntqf
4|19444dLHkrxk|10456FFb^V|21960sGZfse\|19715xquh|32009RfFmA|28303_QygwKx|28444McTs|5352vQFB|17659uofx|26969QEnBd|22372KVXl|30644KatEOd|8183EsFNBNs|26162P^bxe_E|24586eveWZHE|24907JHRLZdg|6834qctb|11977tOXuXB|12554HUX^F|7215_ZahLAa|3589VjHFc|17681PA]vTqb|16490CBTQ|10866Y^Dno\|26235R]srCEF|13897TVdx`J|27500pcigdWb|11664PYsc|4020HgTsBYq
5|26342wCyV|28887CJ\[fPs|16881^pRVuYY|12388JeriY|2979YQ\w|8099qK\]X|29998]eu_`O|27700NYuL|289bMWVe|4607VpJrmKs|18336koiQ|8611vQrxdYW|6912ttnxpRU|2241CENyF[|29767_mEfEN|14521EiHkHg|22504SBwFbz|29515jYCrme|5754GkzrHAw|14375IiIx|1059QxDB|8136L\gt|18302KScaXq|19659FRYB|16752NHox|23132`izp|23499bgjoprd|30650WVxHkz|1676QqKUJf
6|15294oIFZPM|5488EPgR|24406Mmy^|20820OCMt^q|16097][kmq|29440gR^Lvy|26456OEPdPz|27616[tfnXC|13216IKIYAeG|24027fnDA|17957kiPq|28187ySUmCAf|17381OgaQx|15971vzKkk`|3135UTgf|25648iInf|24565QZA_|9200sp[hi[|18362N`cEpqt|2804rmKJ|11554D[WS|5662O]QK`|13908wbICUok|20744Ixlif|29609STKr|23818Cs`Qp_c|20962dkgnf|8591^ReW\E|28911WtMeO[
7|18239uWQFFF|6201`yH]B|5791]L[v|18435jHkOvT|25511^ySzrH[|18056MjE_JF|14876ipSiEP|29780c`YpTuS|26264pnRADf|12127wIQsq|25181Cdxxq]Z|16977AtaIsv|27087^p^B|1344dnNWLv|9555JOkNp|31734WOOn|18917rCrm\j|28826es_O]f|1811A`mJx|19198naaTi|20803c[StHg]|18017U]jt[z|17751vROZwnS|19350tt]x`G|23177NyJuqd|29149auTX|31230WiRcX|13431uedgr[l|6252Zhkxmed
8|19669Ettu^S|1313RLnFQg|19265jGVvs|9997ZvsCgGB|21710Tsdg\x|32553RgvPH|12229xjWYIe|8512^IWkNSm|28498BXIsolk|2299KxvZ|16969yoUUVR|5934uu`K|22919^ZOjVRW|25677_csd]bx|14661wbz_|10185QrmzV[|13051lk_kx|26726[kipm|14861BPuPR|13763dtsLp|11680zhor|26464ZqZFb[S|27281bnAZx|30023QphlfNq|32382_wnOWR|6505VhoajP|23526hhycvC[|22126tjbx^\|12085FLtac
9|31896e]mX|11280S[Okg|31451JpeC|13201rSPJsr|28026pLxYgW|1228Ma[O|21756IKkYJFn|21814BYyyhIk|32054VN`j|22057VqEm|13823pdQphy|27089S]utdK|28215TUDMDnv|26276D^\OQ|12959rxlK|688ThznW|9471GiWCPT|26023sNyOjm|18837sOBDM_m|19889pbzFYO|30767XxOCoAE|10377F\bLdp|11542uXXfPj|16480w[UA[x|31384M]s]c|14296E[Iv|31259tUFZl|11865Aj_ib|22757wVYILE

这是我的代码

#include "hybridDDBBHeader.h" // This is a .h with different classes defined by me
// We have a class "HybridDataBase" that contains the attributes vector<Table> pointer
// We have a class "Table" that contains the attributes int id, int type, int columns, and vector<ElementSet> RowsCols
// We have a class "ElementSet" that contains the attributes vector<strings> elements

int main()
{
vector<Table> TablesDescriptor;

    ifstring ifs("C:\\Table\\Table.txt"); 

    int id; 
    int type; // Don't worry about the meaning of this variable
    int columns; // Don't worry about the meaning of this variable
    vector<string> buffer;
    vector<ElementSet> RowsCols;

    string temp; // Variable where the first row will be stored

    getline( ifs, temp ); // We get ONLY the FIRST LINE of my ".txt" file and we work with it. We take it from my file ifs and we store this first line in string temp

    size_t tokenPos = temp.find("|"); // When we find the simbol "|" we will identify different elements
    int i=0;

    while (tokenPos != string::npos)
    {    
        tokenPos = temp.find("|");   
        if(i==0)
        {
            id = atoi(temp.substr(0, tokenPos).c_str()); 
        }
        if(i==1)
        {
            type = atoi(temp.substr(0, tokenPos).c_str()); 
        }
        if(i==2)
        {
            columns = atoi(temp.substr(0, tokenPos).c_str()); 
        }
        temp.erase(0, tokenPos+1);
        i=++i;
    }

    // We continue reading the NEXT LINES of our code UNTIL THE END of the file
    while(getline( ifs, temp )) 
    {
    size_t tokenPosition = temp.find("|"); // When we find the simbol "|" we will identify different elements

        while (tokenPosition != string::npos)
        {    
            string element;
            tokenPosition = temp.find("|");      

            element = temp.substr(0, tokenPosition);

            buffer.push_back(element);

            temp.erase(0, tokenPosition+1);
        }

        ElementSet ss(0,buffer);
        RowsCols.push_back(ss); 
    }

    Table TablesStorage (id, type, columns, RowsCols); // After every loop we will store the information about every .txt file in the vector<Table> TablesDescriptor
    TablesDescriptor.push_back(TablesStorage);  // In the vector<Table> TablesDescriptor will be stores all the different tables with all its information
}

HybridDataBase hybriddatabase(1, TablesDescriptor);


cout<<"--- SOLUTION ---"<<endl;
ElementSet selectedElementSet;
Table selectedTable = hybriddatabase.getPointer().at(0); // This works perfectly
selectedTable.setType(1); // This works perfectly, but don't worry about this
selectedElementSet=selectedTable.getRowsCols().at(3);  // PROBLEEEEEEM!!!!!!!!!!!! It doesn't read information from row 3, always row 0

cout<<"selectedElementSet: "<<selectedElementSet.getElements().at(0)<<endl; // This works perfectly

我会等你的帮忙。非常感谢你提前!!! :)

1 个答案:

答案 0 :(得分:1)

ElementSet ss(0,buffer);之后您可能想要buffer.clear();

此外,这是毫无意义的:i=++i;。使用++i;