如何在文件中的特定行上书写? [英] How to write on a specific line in a file?

查看:77
本文介绍了如何在文件中的特定行上书写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在已经存在的文件上写入一些数据。它是一个包含大约8-10行标题(#注释),然后成千上行的数据值的文件。我想要的是保持头相同,但添加更新的数据值到文件。很可能在更新后,我有更少的行数据值

I want to write some data on an already existing file. It is a file that contains about 8-10 lines of header(# comments) and then thousands of lines of data values. What i want is to keep the header same but add the updated data values to the file. It is quite possible that after the update I have less number of lines of data values.

删除标题中最后一个#注释后的所有内容,然后从那里开始写入新值。这是可能吗?

So basically i want to erase everything after the last # comment in the header and then start writing the new values from there onwards. Is that possible?

以下是一个例子:


strong>原始文件



#Program
#Date
#Hello
0 23 23 54
1 12 4 2
2 253 786 9887
3 3 23 54
4 1 4 4
5 23 6 81




Updated File



#Program
#Date
#Hello
0 2 23 54
2 253 786 9887
5 23 6 81

我编辑的代码是使用 fopen 读取文件, fprintf 它。我更喜欢如果答案是沿着这些线,所以我不必改变这两个。

The code i am editing is using fopen to read the file and fprintf to write to it. I would prefer if the answers are along these lines so that i don't have to change those two.

推荐答案

我想出了打开原始文件,读取和复制头到内存中,如字符串头。然后通过写覆盖整个文件,然后新数据

The simplest way I came up with is open the Original File, read and copy the header in to memory such as a string header. Then overwrite the whole file by writing the header, then the new data

这篇关于如何在文件中的特定行上书写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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