Hi, this is a code snippet from a part of a little game project I am working on, it comes from randomly generated towns that can have dungeons inside them
This code runs after a dungeon has been cleared, it opens the towns file, skips until it reaches the line in the code where the dungeon is and then sets the dungeon to equal its name + (cleared) to show it has been cleared and removing the excess numbers at the end of that line of code for the buildings information.
My problem is I have no clue how to edit using ofstream without overwriting everything, the getline thing I'm using wont compile because its for ifstream but I don't know how to skip all the way to line 10 without changing lines 1-9, I don't know how to only change the one line and I can't seem to find anything on google that fits my issue.
As you've noted you can't insert text into a file anywhere but the end of file without overwriting existing text. Your best bet is to read the file into a vector and then do the modifications in memory and then re-write the file.
Ok, thanks for the reply, I've never used vectors before so I'm not sure how I would go about following your advice, any suggestions on how I would do it?
std::vector<Data> Names;
ofstream CreateTown (TownName + ".txt");
Randomiser = TownLocation;
int id = 0;
int element = 0;
std::string name;
while(Randomiser != 1)
{
Randomiser --;
// create element
Names.push_back(Data());
// populate it
Names[element].id = id;
Names[element].name = name;
element++;
}
CreateTown.close();
I've had a look at it but I'm still not 100% on how it works, from your post this is what I've made of it, I'm not sure what part of this I would insert code to replace the line I want.
And sorry for the noobness of my replies but I genuinely have such little knowledge of how to use vectors that looking at the stuff you linked feels like calculus to a 5 year old haha
Well, Data in line 1 is pointing to my structure, what does your structure look like ?, and what you need to be doing is populating the vector with the file contents and not the id and name, it looks like you kinda just copied parts of my code :p
I'm sure there is a realtively straight forward way I just have no idea what it is, I also have no idea how to feed the stuff from the text file into a vector.