【问题标题】:Making text files into arrays将文本文件制作成数组
【发布时间】:2015-05-26 15:50:44
【问题描述】:

以下是我项目的总体要点: 我有三个文件:file1、file2 和 file3。这些文件里面是一堆电子邮件。 file1 应该包含所有电子邮件,其中 file2 有一组,file3 有另一组。我需要编写一个程序来读取这些文件,查看这 3 封电子邮件中缺少哪些电子邮件,然后写入一个名为主文件的新文件,主文件的输出必须告诉我每个电子邮件中缺少哪些电子邮件文件。

例如,假设 Anna 在 file2 中,但在 file1 中找不到 Anna。主文件的输出应该告诉我需要将 Anna 添加到 file1(我知道 Anna 不是电子邮件,但这只是一个示例。)

下面是我一直在处理的代码,以及用于查找和/或编写文本文件的代码。我的问题是如何获取这些文件并将它们放入一个数组中,以便对它们进行排序并获得我想要的输出。或者如果这不是你的处理方式,我必须如何处理。

    class Program
    {
const string file1 = "file1.txt";
const string file2 = "file2.txt";
const string file3 = "file3.txt";

static void Main(string[] args)
{
            try
    {
     var count = File.ReadLines(file1).Count();
        //This code simply checks if the full_pack file exists and if it does, it will give how many lines are in the file
        if (!File.Exists(file1))
        {
            Console.WriteLine("File Not Found");
        }

        else
        {
            string[] lines = System.IO.File.ReadAllLines(file1);
            Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file1));
            Console.WriteLine(lines.Count());

        }

        //this code is similar to the one above, however it is for den1
        if (!File.Exists(file2))
        {
            Console.WriteLine("File Not Found");
        }
        else
        {
            string[] lines = System.IO.File.ReadAllLines(file1);
            Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file2));
            Console.WriteLine(lines.Count());
        }

        //this code is similar to the one above, however it is for den2
        if (!File.Exists(file3))
        {
            Console.WriteLine("File Not Found");
        }
        else
        {
            string[] lines = System.IO.File.ReadAllLines(file3);
            Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file3));
            Console.WriteLine(lines.Count());
        }

        //This code is to create a new file called masterfile
        string masterFile = @"C:\Users\povermyer\documents\visual studio 2013\Projects\BoyscoutProject2\BoyscoutProject2\Data\masterFile.txt";
        if (!File.Exists(masterFile))
        {
            File.Create(masterFile);
            TextWriter tw = new StreamWriter(masterFile);
            tw.WriteLine("Here is the list of missing emails");
            tw.Close();
        }
        else if (File.Exists(masterFile))
        {
            TextWriter tw = new StreamWriter(masterFile);
            tw.WriteLine("Here is the list of missing emails");
            tw.Close();
        }

    }

    catch (Exception ex)
    {

        Console.WriteLine(ex.ToString());
    }
    finally
    {
        Console.ReadKey();
    }

【问题讨论】:

  • 是的,用 c# 标记它通常也会给我们这些信息。
  • 关于主题:您已经有了string[] lines = System.IO.File.ReadAllLines(file1); 行,它将所有电子邮件放入一个数组中,那么问题是什么?
  • 它将它放入数组是的,我需要知道如何从这三个文件中获取所有信息,查看这些文件中缺少哪些电子邮件,然后输出主文件阅读我需要从哪个文件添加到 file1 的电子邮件。
  • 感谢所有在这个漫长的编码过程中提供帮助的人。我感谢每个人为编写此代码所做的所有辛勤工作!

标签: c#


【解决方案1】:

HadiRj 使用HashSet 有一个好处,但逻辑是错误的。您需要从文件 2 和 3 中删除当前在文件 1 中的所有记录,因此将保留在文件 2 和 3 中的记录首先是文件 1 中没有的记录:

string[] file1 = System.IO.File.ReadAllLines("file1.txt");
string[] file2 = System.IO.File.ReadAllLines("file2.txt");
string[] file3 = System.IO.File.ReadAllLines("file3.txt");

HashSet<string> missingFromTwo = new HashSet<string>(file2);
HashSet<string> missingFromThree = new HashSet<string>(file3);

//this line will remove all etries in file1 from file2,
//hence file2 will remain with entries that do not exist
//in file one.
missingFromTwo.ExceptWith(file1);

//this line will do the same with file3.
missingFromThree.ExceptWith(file1);

StringBuilder FinalResult = new StringBuilder();
FinalResult.AppendLine("emails from file 2 that need to be added to file1:");
FinalResult.Append(String.Join(Environment.NewLine, missingFromTwo.ToArray()));


FinalResult.AppendLine("emails from file 3 that need to be added to file1:");
FinalResult.Append(String.Join(Environment.NewLine, missingFromThree.ToArray()));

System.IO.File.WriteAllText("Master.txt", FinalResult.ToString());

【讨论】:

  • 最后一行代码有问题。不管我做什么,都会给我一个重载的错误
  • @PatrickOvermyer 最后一行只是将其全部输出到文件中。我是从记忆中写的,只是谷歌如何将字符串生成器输出到文件。
  • 这很棒。我相信这终于是我一直在寻找的代码。非常感谢您的所有帮助,我非常感谢它
【解决方案2】:

根据我从您的问题中了解到的情况,我在这里编写了这段代码而没有经过测试。希望它能给你一些想法。

string[] file1Content = System.IO.File.ReadAllLines("file1.txt");
string[] file2Content = System.IO.File.ReadAllLines("file2.txt");
string[] file3Content = System.IO.File.ReadAllLines("file3.txt");
var file2Missings = file2Content.Except(file1Content);
var file3Missings = file3Content.Except(file1Content);
string[] MasterFileContent = file2Missings.Concat(file3Missings).Distinct().ToArray();

foreach(string s in MasterFileContent.Distinct().ToArray())
      Console.WriteLine(s + " must be placed in file 1"); 

更新

你可以看到它在这里运行:http://goo.gl/3fP56T

【讨论】:

  • 您能解释一下您使用 MasterFilePath 的目的是什么吗?
  • @PatrickOvermyer MasterFilePath 是主文件的路径。将其替换为您提供的有问题的 C:\Users\povermyer\documents\visual studio 2013\Projects\BoyscoutProject2\BoyscoutProject2\Data\masterFile.txt 路径。
  • 代码只是给了我所有我已经拥有的电子邮件。我需要它来告诉我哪些电子邮件不在哪个文件中。就像我在问题中所说的那样:如果 Anna 在 file2 中但不在 file1 中,则输出必须说“安娜需要放在 file1 中”
  • @PatrickOvermyer 我更新了我的代码。我想这就是你想要的。不是吗?
【解决方案3】:

您可以使用以下代码: 已编辑

        //Check if files are available
        var file1 = File.ReadAllLines("file1.txt");
        var file2 = File.ReadAllLines("file2.txt");
        var file3 = File.ReadAllLines("file3.txt");
        var result = "result.txt";
        var ThreeAndTwo = file3.Concat(file2);
        foreach (var email in ThreeAndTwo)
        {
            if (!file1.Contains(email))
                File.AppendAllText(result, email + " needs to be placed in file1" + Environment.NewLine);
        }

编辑 2 展示结果

file1.txt 的内容

测试1

测试2

测试3

测试4

测试5

file2.txt 的内容

测试2

测试22

测试22

file3.txt 的内容

测试3

test33

test333

result.txt 的内容

test22需要放在file1中

test222需要放在file1中

test33需要放在file1中

test333需要放在file1中

【讨论】:

  • 这是否说明文件 1 中缺少此电子邮件
  • 这个解决方案正好相反。它只返回所有 3 个文件中存在的电子邮件,而不是丢失的。
  • @HadiRj 你的代码实际上没有做任何事情:(当你运行它时代码运行但文本没有进入文件
  • @PatrickOvermyer 我添加了使用我的代码得到的结果,您可能检查了错误的文件!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 2022-11-16
  • 2016-07-23
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多