Jump to content
  • Advertisement
Sign in to follow this  
MTclip

vectorchar* or vectorchar[40]

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey ok i am tring to keep a vector of char[40]....... i am reading in a string as char[40] and tring to push it in to the vector

std::vector<char*> myLevels;	
char levelFileName[40];
inFile >> levelFileName;
myLevels.push_back(new char[40]);
myLevels.at(level) = levelFileName;


I have tried many different things but .. inknow the the infile gets the data.. but i cant seem to get this to work..

Share this post


Link to post
Share on other sites
Advertisement
ok, strcpy seems like what you need to do. Right now, you're just getting massive memory leeks by assigning a new value to the stored pointer in the vector.

That is to say: you're copying a POINTER not the DATA. Change it to this and it should work:
std::vector<char*> myLevels;
char levelFileName[40];
inFile >> levelFileName;
myLevels.push_back(new char[40]);
strcpy (myLevels.at(level), levelFileName);


of course, you REALLY should be using std::string... it's obvious you're using c++ with std::vector, why make life hard on yourself?

std::vector<std::string> myLevels;
std::string levelFileName;
inFile >> levelFileName;
myLevels.push_back(std::string);
myLevels.at(level) = levelFileName;


EDIT: what the heck is level?

Share this post


Link to post
Share on other sites
... oh thanks for that ..your a life saver...seriously..

any ways level was my variable in the for loop
for(int level = 0; level < NumberofLevels; level++)

to me it makes for sense than i...

Share this post


Link to post
Share on other sites
If the idea is to just build a list of each name seen, then don't try to push back an empty value and then assign to it from the real value. Just assign the real value:


std::vector<std::string> myLevels;
for (int level = 0; level < numberOfLevels; ++level) {
std::string levelFileName;
inFile >> levelFileName;
myLevels.push_back(levelFileName);
}


The push_back operation will simply copy the current value into a new "slot" at the end, and the file name will be reset in each iteration by the read.

Edit: That said, you couldn't do it with the char[40], because push_back would get passed a pointer to the array, which is what would be copied. You would end up wit h a vector full of copies of pointers to the same (local) char buffer, which at the end would contain whatever the last filename was (and then make things even worse by promptly going out of scope). If you're smart enough to be using std::vector, you should definitely be smart enough to use std::string. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!