Jump to content
  • Advertisement
Sign in to follow this  
bengaltgrs

Copying character arrays

This topic is 4884 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

Basically I am trying to read in a file line by line and store each line into an array as follows. char TempObjectName[32]; vector<char*> CompleteObjectList (0); ifstream infile; infile.open("ObjectList.dat"); while(!infile.eof()) { static short int i = 0; infile.getline(FirstWord, '\n'); strcpy(CompleteObjectList, FirstWord); cout << CompleteObjectList << endl; i++; } infile.close(); The code compiles fine, but a runtime error occurs when I try to copy the array of characters into the CompleteObjectList. Any help is much appreciated. -Chris

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by bengaltgrs

strcpy(CompleteObjectList, FirstWord);
cout << CompleteObjectList << endl;


First, you haven't created any items in the vector CompleteObjectList. You initialize it's length to zero items, so accessing any item in it is a no-no. Second, even if you did make it of any length, you haven't allocated any memory for the pointer contained in the vector, so the pointer contains a garbage address and accessing what it points to is a no-no aswell.

First you need to resize the vector so is't large enough to hold enough items for the index you use to access the vector with, and then you must allocate enough memory for the pointers in the vector. But since you're using vector, I see no reason you you're not using string aswell.

Something like this. Not tested so may not work without some modifications.

vector<string> CompleteObjectList (0);

ifstream infile;
infile.open("ObjectList.dat");

while(!infile.eof())
{
string n;
getline(infile, n);

CompleteObjectList.push_back(n);
}
infile.close();

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Dynamic Arrays ;)

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!