在C读写大文件的有效方法 [英] Effective methods for reading and writing large files in C

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

问题描述

我正在写非常大的用户生成的输入文件涉及的应用程序。该程序将复制有关文件的95%,有效地复制它,并在副本交换了几句话和值,然后追加拷贝(块中)到原始文件,使得每个块(包括10和50之间线)在原来的后面是复制和修改块,然后在下一个原始块,依此类推。用户产生的输入,符合一定的格式,并且它是非常不可能的在原始文件中的任何线长超过100个字符。

I'm writing an application that deals with very large user-generated input files. The program will copy about 95 percent of the file, effectively duplicating it and switching a few words and values in the copy, and then appending the copy (in chunks) to the original file, such that each block (consisting of between 10 and 50 lines) in the original is followed by the copied and modified block, and then the next original block, and so on. The user-generated input conforms to a certain format, and it is highly unlikely that any line in the original file is longer than 100 characters long.

这将是更好的办法?


  1. 要使用一个文件指针,并使用该保持的多少已被读出并在这里写入,求文件指针来回读写当前位置变量;或

  1. To use one file pointer and use variables that hold the current position of how much has been read and where to write to, seeking the file pointer back and forth to read and write; or

要使用多个文件指针,一个用于读取,一个用于写入。

To use multiple file pointers, one for reading and one for writing.

我主要关心的程序的效率,作为输入文件将长达到高达25000行,每行约50个字符。

I am mostly concerned with the efficiency of the program, as the input files will reach up to 25,000 lines, each about 50 characters long.

推荐答案

25000行* 100个字符= 2.5MB,这是不是一个真正的巨大的文件。最快可能会读取内存中的整个文件并将结果写入到一个新的文件,并使用替代原来的。

25000 lines * 100 characters = 2.5MB, that's not really a huge file. The fastest will probably be to read the whole file in memory and write your results to a new file and replace the original with that.

这篇关于在C读写大文件的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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