reading txt file into a vector

Hello,
Im tring to write a function which get filename.txt and insert the file values into a 1D or 2D vector. it does work for the 1D vectors, but i get an error for the 2D.
my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void main(){
    int n=500;
    vector<vector<float> > L ( n, vector<float>(n)); //2D vector
    readFromFile(&L[0][0],"test.txt",n*n); //read from file function
}

void readFromFile(float* dst,const char* fileName,int length){
	
	float value=0.0;
	int index=0;
	ifstream myFile;
	myFile.open(fileName);
	while(!myFile.eof()&&index<length) {
		printf(" %d ",index);
		myFile >> value;
		dst[index++]=value;
	}
	myFile.close();
}


I get the next error:
Unhandled exception at 0x00405757 in cppIntegration.exe: 0xC0000005: Access violation reading location 0x3f800004.

Any advice?
Thanks!!!
are you sure that index not goint over vector length ?
file don't have more that 500 float elements ?
Ziv Mhabary wrote:
Any advice?


Don't use pointers :o)
Thanks but:

Ivan- Im sure i dont have a problem with my file or with vector length.

Galik- Why not to use pointers? its should work.

I think it somehow related to 2D vector arithmetic. if i have a 2D vector M[3][3], then im not sure that *M[3]==M[1][1]
The memory layout of a vector of vectors is the same as an array of pointers to individually-allocated arrays. Given &L[0][0], you can only iterate until &L[0][n-1]. Incrementing that pointer by one drops you off into oblivion and does not give you &L[1][0] as it does with 2D arrays.

And, yes, pointers complicated this task unnecessarily.
Last edited on
Ziv Mhabary wrote:
Galik- Why not to use pointers? its should work.


Pointers are dangerous and lead to access violations. So I advise that you only use pointers when you *have to* use pointers.

In a vector of vectors you can not use pointer arithmetic outside of any single vector. So if you take the address of the first element of a vector &L[0][0] you can only address elements L[0][0] to L[0][n - 1].

You can NOT use pointer arithmetic to treat all the elements as a contiguous lump of memory.
Last edited on
Pointers are dangerous and lead to access violations. So I advise that you only use pointers when you *have to* use pointers.


I would agree that you shouldn't overuse pointers, but don't go out of your way to avoid them either. There are cases where pointers are not necessarily required, but they simplify things a lot. Instead of shying away from pointers, I would recommend to new users that they focus a lot on pointers until they understand when and how it gives an advantage.

Which I agree is not in this case.
Thank you all.

If i wont use pointers, then i have to send the vector as an argument to the function. wouldnt it be much more slower?
closed account (zb0S216C)
Ziv Mhabary wrote:
wouldnt it be much more slower? (sic)

No, it'd be faster, if anything. Passing by value is slower since the entire vector would have to be copied. By using a pointer, or more preferably, a reference, the actual argument is not copied, but pointed to or referred to and no copy operation takes place.

Wazzak
Last edited on
You can pass the vector by reference (using a &) which won't be slower at all. So you might have something a bit like this:

1
2
3
4
5
6
7
8
void readFromFile(std::vector<std::vector<float> >& dst, const std::string& fileName)
{
	std::ifstream myFile(fileName.c_str());

	for(size_t x = 0; x < dst.size(); ++x)
		for(size_t y = 0; y < dst[x].size(); ++y)
			myFile >> dst[x][y];
}
Last edited on
Topic archived. No new replies allowed.