简体   繁体   English

使用文件流删除一行文本(C ++)

[英]Remove A Line Of Text With Filestreams (C++)

I have a large text file. 我有一个大文本文件。
Each time my program runs, it needs to read in the first line, remove it, and put that data back into the bottom of the file. 每次我的程序运行时,它需要读取第一行,删除它,并将该数据放回文件的底部。

Is there a way to accomplish this task without having to read in every part of the file? 有没有办法完成这项任务,而无需阅读文件的每个部分?
It would be great to follow this example of pseudo code: 遵循这个伪代码的例子会很棒:

 1. Open file stream for reading/writing 2. data = first line of file 3. remove first line from file <-- can I do this? 4. Close file stream 5. Open file stream for appending 6. write data to file 7. Close file stream 

The reason I'm trying to avoid reading everything in is because the program runs at a specific time each day. 我试图避免阅读所有内容的原因是因为程序每天在特定时间运行。 I don't want the delay to be longer each time the file gets bigger. 我不希望每次文件变大时延迟都会更长。

All the solutions I've found require that the program process the whole file. 我发现的所有解决方案都要求程序处理整个文件。 If C++ filestreams are not able to accomplish this, I'm up for whatever alternative is quick and efficient for my C++ program to execute. 如果C ++文件流无法实现这一点,那么无论如何,我都可以快速高效地执行C ++程序。
thanks. 谢谢。

The unfortunate truth is that no filesystem on a modern OS is designed to do this. 不幸的是,现代操作系统上没有文件系统可以用来做到这一点。 The only way to remove something from the beginning of a file is to copy the contents to a new file, except for the first bit. 从文件开头删除内容的唯一方法是将内容复制到新文件,第一位除外。 There's simply no way to do precisely what you want to do. 根本没有办法准确地做你想做的事。

But hopefully you can do a bit of redesign. 但希望你能做一些重新设计。 Maybe each entry could be a record in a database -- then the reordering can be done very efficiently. 也许每个条目都可以是数据库中的记录 - 然后可以非常有效地完成重新排序。 Or perhaps the file could contain fixed-size records, and you could use a second file of indexes to specify record order, so that rearranging the file was just a matter of updating the indices. 或者文件可能包含固定大小的记录,您可以使用第二个索引文件来指定记录顺序,因此重新排列文件只是更新索引的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM