如何将3行第一行从文件更改为另一行? [英] How I can change the 3 first line from file to another?

查看:77
本文介绍了如何将3行第一行从文件更改为另一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友

我有*文件,我想用另外2个文件剪切文件

我想从初始文件中提取3个第一行并将其更改为另一个*文件

-之后我想删除初始*文件中的第3行

thnk以获得帮助



我尝试过:



int _tmain(int argc,_TCHAR * argv [])

{

FILE * fpIn = fopen(file_initial,rb);

//创建另一个fie

/ /提取3第一行并更改o文件

//从最初的fil删除第3行

;

end

解决方案

你必须要理解的主要是:一般情况下,你必须重写整个文件。为什么?



如果您查看文本文件的结构并理解什么是line,您将理解它。这是一个由恩d行字符串。如果要更换的其中一个字符串短于或长于更改之前,则更改后的所有文本都应在内存中移位。



文件不是非常大,您可以在内存中读取所有内容,然后更改需要更改的行,并将其写回。如果文件太大而无法可靠地放入内存中,请读取每一行并将其写入新的(临时)文件,更改所需的行。完成后,您可以将临时文件重命名/移动到原始文件。



-SA


hey friends
I have *file , I would like to cut the file by 2 another *file
I want just from the initial file extract the 3 first line and change its to another *file
-after I want delete the 3 first line from the initial *file
thnk for your help

What I have tried:

int _tmain(int argc, _TCHAR* argv[])
{
FILE *fpIn=fopen("file_initial",rb");
//create another fie
//extract the 3 first line and change o the file
//delete the 3 frst line from the initial fil
;
end

解决方案

The main thing you have to understand is: in general case, you have to rewrite the whole file. Why?

You will understand it if you look at the structure of the text file and understand what is "line". This is a string subdivided by end-of-line strings. If one of the strings to be replaced is shorted or longer than before the change, all the text after the change should be shifted in memory.

It the file is not very big, you can read it all in memory, and then change the lines you need to change, and write it back. If the file is too big to reliably fit in memory, read each line and write it in a new (temporary) file, changing required lines. When it's done, you can rename/move the temp file to the original file.

—SA


这篇关于如何将3行第一行从文件更改为另一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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