'std::out of range' error

Im trying to write an interpreter on c++ that will be used in wombat 1.
I got this error on my program when complied:
std::out of range
what():basic_string::substr


1
2
  opcode[line_array] = file.substr(0,4);// Reads the opcode
  operand[line_array] = file.substr(4,12);// Reads the operand 


HELP!
How is file declared? Basically it tells that there is no 16 symbols in it.
How does Opcode+Operand looks like?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
inFile.open ("E:\\file.txt", ios::in);
    
    int r = 0;
    
    
    if (inFile.fail())
      {
       cout <<"File could not be read"<<endl;
      }
    else
      {
       while (!inFile.eof())//
       {
                getline(inFile,file);
                opcode[line_array] = file.substr(0,4);// Reads the opcode
                operand[line_array] = file.substr(4,12);// Reads the operand
           
                line_array++;
               
        }


1
2
string opcode[ARRAYSIZE];
string operand[ARRAYSIZE];


opcode & operand:
1001000000000000
0001000000010000
The error occured in this line

operand[line_array] = file.substr(4,12);// Reads the operand

I think that file.length() is less than or equal to 4. You can check it yourself by outputing file.length() before this line.
okay so its suppose to be substr(len,pos) or substr(pos,len) ?
I wrote all clear. I will not repeat the same twice.
right sorry about that.
You should be sure that starting position in this statement that is 4

operand[line_array] = file.substr(4,12);//

is less than the size of the string.
well the size of the string is 16 so obviously it is less :/
According to the C++ Standard the exceprion out_of_range is generated when

out_of_range if pos > size().
Topic archived. No new replies allowed.