user10078987
user10078987

Reputation:

C++, how to update text in Text file?

I want to create a function for a room booking system. Consider the status of each room as "Not reserved" and there may be some rooms which already booked and the status for them are "Reserved" and they are saved in a text file. so how can I code it that keep the "Reserved" Status as it is in the file and change the status for a room that I want to reserve? and then save the file. Thank you

Something like this:

num             Status
room1           Reserved
room2           Not-Reserved
room3           Reserved

Upvotes: 0

Views: 63

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409136

Since "records" in a text file is generally of mixed length, you can't just seek to a position in the file and rewrite a portion of it. You have to rewrite the whole file.

There is really only a single way to do it: Store the contents in a temporary space, and change what needs to be changed either when reading into the temporary storage, or when writing the temporary storage to the original file.

The temporary storage could either be another (temporary) file that you then rename to the original file. Or it could be memory inside the program itself.


However, there are other solutions to the storage problem that doesn't suffer from the above problem, and those include some kind of database.

There are many kinds and types of databases, together with an even larger amount of libraries to access the databases.

Upvotes: 3

Pratik Rathi
Pratik Rathi

Reputation: 86

You should use database instead of text file for this. or else you can use "xml,json or csv" files which are easy to handle such operations.

Upvotes: 0

Related Questions