写文本到一个文件的中间 [英] Writing text to the middle of a file

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

问题描述

有没有一种方法,我可以从文件中的某一点写文本文件?

Is there a way I can write text to a file from a certain point in the file?

例如,我开的10行文本的文件,但我想要写了一行字,以5日线的。

For example, I open a file of 10 lines of text but I want to write a line of text to the 5th line.

我想一个办法就是使用readalllines方法获取文本文件中的行后面作为一个数组,然后在阵列中某一索引添加一行。

I guess one way is to get the lines of text in the file back as an array using the readalllines method, and then add a line at a certain index in the array.

但有一个区别在于一些集合只能成员添加到端和一些在任何目的地。要仔细检查,数组总是让我在任何索引添加值,对不对? (我敢肯定,我的书另一个聪明的说)。

But there is a distinction in that some collections can only add members to the end and some at any destination. To double check, an array would always allow me to add a value at any index, right? (I'm sure one of my books said other wise).

此外,有没有要去这件事?

Also, is there a better way of going about this?

感谢

推荐答案

唉,一声叹息。 ,查找主文件更新算法

Oh, sigh. Look up the "master file update" algorithm.

下面是伪代码:

open master file for reading.
count := 0
while not EOF do
    read line from master file into buffer
    write line to output file    
    count := count + 1
    if count = 5 then
       write added line to output file
    fi
od
rename output file to replace input file

这篇关于写文本到一个文件的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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