如何合并2文本文件 [英] How to merge 2 text files

查看:177
本文介绍了如何合并2文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找不同的解决方案,包括那些在禁止使用 .NET库和那些我可以使用所有的这些优势。

I'm looking for different solutions, including those in which is forbidden to use .NET libraries and those where I can use all of the advantages of them.

这是问题所在,我有两个文本文件, textFile1 textFile2 。他们每个人都包含排序的整数(这是最重要的条件),象下面显示:

Here is the problem, I have two text files, textFile1 and textFile2. Each of them contains sorted integer numbers(this is the most important condition), like these displayed below :

textFile1      textFile2
0              1
2              3
4              5

我需要创建第三个文本文件,例如 textFile3 通过合并这两个文件,和预期的结果应该是:

I need to create 3rd text file, for example textFile3 by merging those two files, and expected result should be :

textFile3
0
1
2
3
4
5

我的第一个想法是通过线这两个文本文件的行复制成两个独立的阵列和大于合并中新的两个排序阵列使用的解决方案,提供了
<在这个问题上HREF =http://stackoverflow.com/questions/5958169/how-to-merge-two-sorted-arrays-into-a-sorted-array>。
之后这一点,我将行新数组成员复制到 textFile3 ,行

My first idea was to copy those two text files line by line into two separate arrays and than use solution for merging two sorted arrays in new one, provided in this question. After that, I will copy those members of new array into textFile3, line by line.

你有什么建议?也许更好的办法?请写出你的观点在这里,他们每个人都会有所帮助我。

Do you have any suggestion ? Maybe better approach ? Please write all of your ideas here, each of them will be helpful to me .

推荐答案

合并两个文件是一个相当简单的修改以合并两个数组。我们的想法是与该文件的下一行的读出,以取代在数组索引增量。例如,标准的合并算法,我显示在我的博客( HTTP: //blog.mischel.com/2014/10/24/merging-sorted-sequences/ )是:

Merging two files is a fairly simple modification to merging two arrays. The idea is to replace the array index increment with a read of the next line of the file. For example, the standard merge algorithm that I show in my blog (http://blog.mischel.com/2014/10/24/merging-sorted-sequences/) is:

while (not end of List A and not end of List B)
    if (List A current item <= List B current item)
        output List A current item
        advance List A index
    else
        output List B current item
        advance List B index

// At this point, one of the lists is empty.
// Output remaining items from the other
while (not end of List A)
    output List A current item
    advance List A index

while (not end of List B)
    output List B current item
    advance List B index

要作出这样的合并文件,您可以通过打开和读取每个文件的第一行开始。它得到一种扭曲,但是,因为你必须检查文件的末尾。 。获取下一行是一个有点古怪...

To make that merge files, you start by opening and reading the first line of each file. It gets kind of screwy, though, because you have to check for end of file. "Get the next line" is a bit ... odd.

int item1;
int item2;
bool eof1 = false;
bool eof2 = false;
string temp;
var file1 = File.OpenText(textFile1);
temp = file1.ReadLine();
if (temp == null)
    eof1 = true;
else
    item1 = int.Parse(temp);

// do the same thing for file2



然后,我们可以做标准合并:(!EOF1和放大器;&安培; EOF2)

Then we can do the standard merge:

while (!eof1 && !eof2)
{
    if (item1 <= item2)
    {
        outputFile.WriteLine(item1);
        // get next item from file1
        temp = file1.ReadLine();
        if (temp == null)
            eof1 = true;
        else
            item1 = int.Parse(temp);
    }
    else
    {
        // output item2 and get next line from file2
    }
}
// and the cleanup
while (!eof1)
{
    // output item1, and get next line from file1
}
while (!eof2)
{
    // output item2, and get next file from file2
}

唯一不同的是,这让下一个项目不仅仅是递增数组索引更多地参与。

The only thing different is that getting the next item is more involved than just incrementing an array index.

这篇关于如何合并2文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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