hi all,

ive got this problem, i think it is an interplay of ifstream and vector...

the following code compiles and links without problem, but when i debug i get the message "expression: vector subscript out of range". i know that "out of range" means i am trying to access something which isnt there, and it is true that the element i am trying to access (in the line *here*) isnt there. the point is i dont understand why! if i take the line with *here* out of the code, it reads from the file, and the cout-loop shows the four words. but when i put the *here* line in, it never couts anything! i am very confused how that line, which is AFTER the cout loop, can affect it, or rather what is even earlier (the reading from the file, which enables the cout loop).

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main(){
	ifstream ifs;
	ifs.open("file.txt");
	
	string tok;
	vector<string> vtoks;

	while (getline(ifs,tok)){
		vtoks.push_back(tok);
	}
//	vtoks.push_back("daimler");
//	vtoks.push_back("volkswagen");
//	vtoks.push_back("bmw");
//	vtoks.push_back("daimler");
	
	for (int i=0; i<vtoks.size(); i++){
		cout << vtoks[i] << " ";
	}
	cout << endl;

	cout << vtoks[0]; //**************here*******************

	return 0;
}

the file "file.txt" contains four lines, no spaces:

daimler
volkswagen
bmw
daimler

i suspect the error is an interplay of the reading from the file and the vector accession, because if i comment out the while loop and uncomment the four push_back lines, it works fine.

any help is appreciated a lot.

richard

Recommended Answers

All 8 Replies

Have tried your code out and didn't get any errors. Have you uploaded the version causing the problem?

yes, i posted the exact code that gives me the described errors while debugging. i am using ms visual studio 2008.

edit:
if i dont debug, but just run the .exe after compiling, the program does what its supposed to. but id still like to know why i get the debugger messages, or should i ignore them?

I ran in debug in visual studio 2005. Try doing full rebuild on debug version. Are you sure it finds file.txt, can you see the entries in the vector before you get to *here*?

If you put in a cout << vtoks[1]; after the line in question, what do you get? Do you get "daimler" or "volkswagen"?

Also if you cout << vtoks.size() what do you get, 4 or 5?

If, for some reason, the while loop does not execute at all, then vtoks will not have any elements and the statement

cout << vtoks[0];

will yield undefined behavior (such as the run-time error message you cited).

So I would start by verifying that the file was actually opened correctly and that the loop executes at least once before looking anywhere else.

hey,
thanks for all the comments!

@arkoenig:
youre right, at cout << vtoks[0]; vtoks is empty, and therefore the error is justified. but i cannot see why, in particular, because if i delete the *here* line then cout << vtoks[0]; is no problem. how does this (later) line affect the (earlier) while (getline...) loop??

any idea?

again: the code actually compiles and links correctly, and the .exe does what its supposed to, but the debug process gives an error. which i would like to get rid of, because i dont know whether that will fall back on me later.

If, for some reason, the while loop does not execute at all, then vtoks will not have any elements and the statement

cout << vtoks[0];

will yield undefined behavior (such as the run-time error message you cited).

So I would start by verifying that the file was actually opened correctly and that the loop executes at least once before looking anywhere else.

If you want help you need to listen to what others are telling you.

You do not currently have any error checking there so it is likely that you are not opening the file correctly. As a result, the while loop doesn't run because it has nothing to put in to the vector.

Add some code around your ifs.open() statement to verify that the file is actually opening as you think it is.

Fbody,

youre right, but i also ackknowledge, that the file is not opened correctly, so the error is justified. but the reading of the file and

for (int i=0; i<vtoks.size(); i++){
        cout << vtoks[i] << " ";

(which, by the way, contains the same cout << vtoks[0] that later causes the error)
works perfectly fine if i take the here line out, and i would like to know why that is the case. because in my program i need something like that.

If you want help you need to listen to what others are telling you. Add some code around your file.open() to verify that the file is opening. You do not currently have any error checking there so it is likely that you are not opening the file correctly. As a result, the while loop doesn't run because it has nothing to put in to the vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.