C ++ Insert a line at a specific point in a file

I have a text file that contains high scores for the game in this format:

Name Score Name Score Name Score 

With a file sorted in descending order by result.

I want to insert a new name and its corresponding rating in the right place in the file so that it remains ordered correctly. Any suggestions on how to do this would be greatly appreciated!

For example, with the following file:

 Edward 100 David 90 Sarah 80 Alice 60 

And I want to add.

 Name = Jodi Score = 70 

To the file, so the new file reads:

 Edward 100 David 90 Sarah 80 Jodi 70 Alice 60 

thanks

Currently I have the following code:

 string playerName = pPlayer->GetName(); int playerScore = pPlayer->GetScore(); std::ofstream score("scores.txt", std::ios_base::app | std::ios_base::out); score << "\n" << playerName << " " << playerScore; 

Which only adds the name to the end of the file. I believed that I read the entire file, ordering it, and then turning the old file over. But I do not want to do this, because it can take a long time if the file becomes large.

+1
c ++ string file-io
source share
4 answers

There are several different ways to do this. The easiest to implement is one that reads the entire file into a vector, inserts a new value, and writes a new file.

The second option is to read the file until you find the right place, "mark" where it is in the file (using, for example, istream::tellg() ), then read all the following elements in the vector, update the new record, and write back one after to others.

The third option is to simply keep the list unordered in the file and sort the information when it is read. That way, you can simply add to the end, saving when you write the file again and again.

In fact, I suspect that writing a VERY large file will still be fast enough to make no difference. Modern hard drives will write many megabytes per second, and you will only have a few tens of bytes per line, so you need to have millions of lines in your file before this even changes. For example, I just copied some large files up to 132 MB in size on my computer, and the time taken to read and write 132 MB reached 1.2 s. If each of your records is 26 bytes, that’s 50 million points.

+4
source share

If you carefully determine the presentation type of your data record:

 struct Record { std::string name; int score; friend std::istream& operator>>(std::istream& is, Record& r) { return is >> r.name >> r.score; } friend std::ostream& operator<<(std::ostream& os, Record const& r) { return os << r.name << "\t" << r.score; } bool operator<(Record const& other) const { return other.score < score; } }; 

Notice he knows how

  • read record from stream
  • write it to the stream
  • compare scores

Then C ++ algorithms will again become your friend:

 int main() { std::ifstream ifs("input.txt"); std::vector<Record> const insert { Record { "Jodi", 70 } }; std::merge( std::istream_iterator<Record>(ifs), {}, insert.begin(), insert.end(), std::ostream_iterator<Record>(std::cout, "\n")); } 

Watch Live On Coliru

+2
source share

How it behaves depends on the file system. There is no solution for the generator. But I do not think that there is a real file system in which you can do such things.

About how the file will look on disk before and after such an operation. A file is one large block on disk. Therefore, rewriting is an action that must be performed.

There may be a solution if you are open to different approaches. Think of this file as memory. How would you do it if not for the disk? What data structure can you use inside the array (we could talk about the file as an array)?

If you do not want to create your own, use sth, which will be executed. Personally, I would use a database for such a task. This is exactly what you want. You can insert data and then sort it and work fast. It is optimized for use in environments such as a hard drive. If you want to have it in a file, you can use SQLite .

0
source share

One way is to download the file and then overwrite it with the new data:

1) You must load the file into the structure using the C ++ file processing API (or any that may seem better to you).

2) Data will be due to sequential loading into the structure. Then you must add node (your new data) to the structure in the order in which it will be sorted.

3) Finally, rewrite the file from the structure.

In your case, you can use std::vector . You can open the file and load all its lines into this vector. You can isolate data by performing string manipulations. Then put your data in a structure so that they are sorted. You can, for example, split a string and then accept a score, parse it as an int and compare with a new data score. After that, the structure contains your data state. Open the file and rewrite it line by line.

0
source share

All Articles