覆盖上的文本文件中的特定行? [英] overwriting a specific line on a text file?
本文介绍了覆盖上的文本文件中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么去覆盖上在C文本文件中的特定行?我在需要被写入到文件中的多个变量的值。
how do I go about overwriting a specific line on a text file in c?. I have values in multiple variables that need to be written onto the file.
推荐答案
这只有当新行的大小与旧相同的作品:
This only works when the new line has the same size as the old one:
- 打开文件中的模式
A +
-
fseek的()
来的文件的开头 - 读取下一行之前,使用
FTELL()
要注意行的开头 - 读取行
- 如果这是你想要的路线,
fseek的()
再从FTELL结果()
,并使用的fwrite()
来覆盖它。
- Open the file in the mode
a+
fseek()
to the start of the file- Before reading the next line, use
ftell()
to note the start of the line - Read the line
- If it's the line you want,
fseek()
again with the result fromftell()
and usefwrite()
to overwrite it.
如果该线的长度发生变化,则必须复制文件。
If the length of the line changes, you must copy the file.
这篇关于覆盖上的文本文件中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文