Editing a line in a text file

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
ofstream CreateTown (TownName + ".txt");

Randomiser = TownLocation;
while(Randomiser != 1)
{
   getline(CreateTown, line);
   Randomiser --;
}

line = BuildingName + " (Destroyed)";
CreateTown << line;

CreateTown.close();


But to sum it up I have a file like this

-------Town.txt--------
Old Shop 0.213.23.32
Gold Mine 0.12.23
Mine Shop 0.12
Dungeon 0.1.12.3
Shop 0.12.123

And I want it to become

Old Shop 0.213.23.32
Gold Mine 0.12.23
Mine Shop 0.12
Dungeon (Destroyed)
Shop 0.12.123
Last edited on
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?
Last edited on

I did a vector example in a previous post for another user, it may be helpful.
http://www.cplusplus.com/forum/beginner/166582/#msg838341

More info on vectors: http://www.cplusplus.com/reference/vector/vector/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
Last edited on

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'll be honest I wasn't sure which parts were essensial for the thing to work so I didn't want to delete variables,

Town.txt looks like this inside

1
2
3
4
5
Old Shop 0.213.23.32
Gold Mine 0.12.23
Mine Shop 0.12
Dungeon 0.1.12.3
Shop 0.12.123


All I want to do is edit one row of it to become this

1
2
3
4
5
Old Shop 0.213.23.32
Gold Mine 0.12.23
Mine Shop 0.12
Dungeon (Destroyed)
Shop 0.12.123


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.
Last edited on
Topic archived. No new replies allowed.