简体   繁体   中英

c++ error reading values from file

This is my first post so if I have written or formated something against forums rules please show some understanding. I also happens to be a novice programmer in C++ so if you could please write somewhat simplified if possible.

I need to read some values from a file named text.txt which is like that (total 13 columns):

1 2 B5 AA 120 0 100 1.5 1.5 G 0 0 0 \n
1 2 B6 AB 120 0 100 1.5 1.5 G 0 0 0 \n
1 2 B7 AC 120 0 100 1.5 1.5 G 0 0 0 \n
...

The above file can have an undetermined number of lines. It is generated through an other routine.

I made a routine in code::blocks. I have made three matrices holding the above data as integers, doubles, strings. The deceleration I have made is as follows:

int c1, c2, c7, i = 0, nNumPipes;
double c5, c6, c8, c9, c11, c12, c13;
string c3, c4, c10;

int PipeI[][3] = {0};
double PipeD[][6] = {0};
string PipeS[10000][3];

Then I use the following syntax to read the lines:

ifstream inf("C:\\text.txt");
while (inf >> c1 >> c2 >> c3 >> c4 >> c5 >> c6 >> c7 >> c8 >> c9 >> c10 >> c11 >> c12 >> c13)
{
    PipeI[i][0] = c1;      
    PipeI[i][1] = c2;      
    PipeS[i][0] = c3;       
    PipeS[i][1] = c4;      
    PipeD[i][2] = c5;       
    PipeI[i][2] = c7;      
    PipeD[i][0] = c8;       
    PipeD[i][1] = c9;     
    PipeS[i][2] = c10;    
    PipeD[i][5] = c11;   
    PipeD[i][3] = c12;     
    PipeD[i][4] = c13;    

    i++;
}

nNumPipes = i;

The code however fails in this 'while' loop. Do I miss something?

The error message I get is a window saying "xxx.exe has stop working".

Any help will be appreciated.

you get this error because your arrays PipeI and PipeD are just one index long in their first brackets so its like

PipeI[1][3]={0};
PipeD[1][6]={0};

if you thry to iterate through it you get the errors. Arrays always have fixed size in c++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM