C ++在文件中的特定点插入一行 [英] C++ Inserting a line at specific point in file

查看:533
本文介绍了C ++在文件中的特定点插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中包含以下格式的游戏的高分:

 名称分数
名称分数
名称分数

分数排序的文件。 >

我想在文件中的正确位置插入新名称及其相应的分数,以便它保持正确排序。



例如,给出以下文件:

  Edward 100 
David 90
Sarah 80
Alice 60

我想添加。

  Name = Jodi 
Score = 70

到文件,因此新文件为:

  Edward 100 
David 90
Sarah 80
Jodi 70
Alice 60

感谢



目前我有以下代码:

  string playerName = pPlayer-> GetName(); 
int playerScore = pPlayer-> GetScore();

std :: ofstream score(scores.txt,std :: ios_base :: app | std :: ios_base :: out);

score<< \\\
< playerName<< < playerScore;

这只是将名称添加到文件的结尾。我考虑在整个文件中读取,然后通过正确的旧文件。但是我不想这样做,因为如果文件很大,它可能需要很长时间。

解决方案

有几种不同的方法可以做到这一点。最简单的实现是将整个文件读入向量,插入新值并写出新文件。



第二个选项是读取文件,直到找到正确的位置,mark位于文件中(例如使用 istream :: tellg()),然后将所有以下元素读入一个向量,更新新的记录,并写回一次。



第三个选项是保持列表在文件中无序,并在阅读时对信息进行排序。这样,你可以只是追加到最后,节省了一遍又一遍地写文件。



在现实中,我怀疑,编写一个非常大的文件仍然会足够快,以至于根本没有什么区别。现代硬盘驱动器每秒写入几兆字节,每行只有几十个字节,所以你需要在文件中有几十行字符,然后再做出任何区别。例如,我只是复制了一些大型文件在我的机器上132MB,而编译时间读写132mb来到1.2s。如果你的每个记录都是26字节,那就是5000万分数。


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

Name Score
Name Score
Name Score

With the file ordered in descending order on score.

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

For Example, given 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

At the moment 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 just adds the name to the end of the file. I considered reading in the whole file ordering it then Over righting the old file. But I don't want to do this because it could take ages if the file got large.

解决方案

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

The second option is to read the file until you find the right place, "mark" where that is in the file (using for example istream::tellg()), then read all the following elements into a vector, update the new record, and write back out the once beyond that.

A third option is to just keep the list unordered in the file, and sort the information when you read it. That way, you can just append to the end, saving on writing the file over and over again.

In reality, I suspect that writing a VERY large file will still be quick enough to make little or no difference at all. Modern hard drives will write many megabytes per second, and you only have some dozens of bytes per line, so you need to have milions of lines in your file before it makes any difference at all. For example, I just copied some large files amounting to 132MB on my machine, and the compined time to read and write 132mb came to 1.2s. If each of your records are 26 bytes, that's 50 million "scores".

这篇关于C ++在文件中的特定点插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆