Sign in to follow this  
31337noob

vector help

Recommended Posts

why is the whole vector being the last thing buffer was when i am pushing back something? push_back should add not same the same.
//example

vector<int> b;

b.push_back(0);
b.push_back(1);

for(int i = 0; i < b.size(); i++)
   cout <<b[i]<<endl;

//you should see
//0
//1


but when i do this,
ZeroMemory(&buffer,sizeof(buffer));
fscanf(file,"%s",buffer);
var.push_back(buffer);
ZeroMemory(&buffer,sizeof(buffer));


the last thing buffer was is "[END]"

so here is what var looks like

var[0] = "[END]"
var[1] = "[END]"
var[2] = "[END]"
var[3] = "[END]"


why?

it is supposed to be

var[0] = "height"
var[1] = "width"
var[2] = "refreshrate"
var[3] = "fullscreen"

why is it doing that?

please help me.

thanks in advanced.

Share this post


Link to post
Share on other sites
First off, your fscanf is not in a loop.
SO you are only reading in one thing, and pushing one thing into the vector.

That said, var[3] should be giving you an error.
But it isn't so it appears to be just returning the only element that is
in the vector.

how are you defining your vector in the second case?
vector<char *> or vector<string>?
The first one may cause a problem, since you are storing pointers to buffer, not the contence of buffer. But the second one shouldn't cause problems.
(though you do zero buffer.... so not sure if that is your problem)

Though i should also note, ZeroMemory takes a pointer, if buffer is defined as
char *buffer or as char buffer[256] , then you should be calling
ZeroMemory ( buffer, 256 );
^Notice the lack of & ? <---- almost sure this is correct, may be wrong though, i never used the function before. You should check that it acutally zeros out the buffer like you think.
^And notice the lack of "sizeof"
since sizeof() does NOT return the length of the buffer if buffer is on the heap. (ie, char *buffer = new char[256];)

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
First off, your fscanf is not in a loop.
SO you are only reading in one thing, and pushing one thing into the vector.

That said, var[3] should be giving you an error.
But it isn't so it appears to be just returning the only element that is
in the vector.


yes it is, it is in a loop. in fact it is in a while(!feof(file))
i didnt send the whole code

vector<char*>

oh, btw buffer is a char buffer[MAX_PATH] = {0}; above the while loop

Share this post


Link to post
Share on other sites
vector<char *>

you are storing a pointer to buffer.
so ever element has a pointer to buffer.
NOT the contense of buffer.

If you use a
vector<string>
that should remove the problem.

^also, note above, my rant about using sizeof(), it isn't safe to assume it gives you the length of an array.

Share this post


Link to post
Share on other sites
As KulSeran said, you should probably use a vector of strings. If you wanted to use a vector of char*'s, you would have to allocate a new buffer for each push_back, then remember to free them.


//read from file
...

//allocate new buffer of size read in
char* buf = new char[size];

//copy to buffer
...

//add buffer to vector
a.push_back(buf);


Then use it.. When you're done you'll have to free each element of a. I'd recommend just using the strings..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this