【问题标题】:Why do I only get results from my first txt?为什么我只能从我的第一个 txt 中获得结果?
【发布时间】:2022-01-10 11:16:28
【问题描述】:

我正在尝试读取和使用 txt 文件中的所有行。使用一种方法,我遍历它们 asinc,并尝试获取数据。我的问题是,输出看起来只包含第一个 txt 文件中的数据。我只是找不到问题所在。我将不胜感激。

这是我的代码:

string[] files = Directory.GetFiles("C:/DPS-EDPWB05/forlogsearch", "*", SearchOption.AllDirectories);

//data I need from the txts
            List<string> num = new List<string>();
            List<string> date = new List<string>();
            List<string> time = new List<string>();
            List<string> sip = new List<string>();
            List<string> csmethod = new List<string>();
            List<string> csuristem = new List<string>();
            List<string> csuriquery = new List<string>();
            List<string> sport = new List<string>();
            List<string> csusername = new List<string>();
            List<string> cip = new List<string>();
            List<string> csuseragent = new List<string>();
            List<string> csreferer = new List<string>();
            List<string> scstatus = new List<string>();
            List<string> scsubstatus = new List<string>();
            List<string> cswin32status = new List<string>();
            List<string> timetaken = new List<string>();

        int x = 0;
        int y = 0;
        int i = 0;
        int filesCount = 0;
        string v = "";

        //Taking the data from the Log, getting a list of string[] 
        //items with the lines from the txts

        List<string[]> lines = new List<string[]>();

        while (i < files.Length)
        {
            lines.Add(ReadAllLinesAsync(files[i]).Result);
            i++;
        }

        //Trying to get the data from the string[]s
        do
        {
            string line;
            int f = 0;
            string[] linesOfTxt = lines[filesCount];

            do
                {
                line = linesOfTxt[f];

                string[] splittedLine = { };

                    splittedLine = line.Split(' ', 15, StringSplitOptions.None);
                        
                    y = splittedLine.Count();

                    if (y == 15)
                    {
                        num.Add(x.ToString());
                        date.Add(splittedLine[0]);
                        time.Add(splittedLine[1]);
                        sip.Add(splittedLine[2]);
                        csmethod.Add(splittedLine[3]);
                        csuristem.Add(splittedLine[4]);
                        csuriquery.Add(splittedLine[5]);
                        sport.Add(splittedLine[6]);
                        csusername.Add(splittedLine[7]);
                        cip.Add(splittedLine[8]);
                        csuseragent.Add(splittedLine[9]);
                        csreferer.Add(splittedLine[10]);
                        scstatus.Add(splittedLine[11]);
                        scsubstatus.Add(splittedLine[12]);
                        cswin32status.Add(splittedLine[13]);
                        timetaken.Add(splittedLine[14]);

                        x++;
                    }
                
                f++;

                } while (f < linesOfTxt.Length);
            filesCount++;
        }
        while (filesCount < files.Count());

在这一切之后,我对这些和东西进行了分组,但这是在我需要的数据列表被填满之后发生的——所以问题一定出在某个地方。另外,我的 asinc 阅读器(我在 stackoverflow 上找到):

public static Task<string[]> ReadAllLinesAsync(string path)
        {
            return ReadAllLinesAsync(path, Encoding.UTF8);
        }

        public static async Task<string[]> ReadAllLinesAsync(string path, Encoding encoding)
        {
            var lines = new List<string>();

            // Open the FileStream with the same FileMode, FileAccess
            // and FileShare as a call to File.OpenText would've done.
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
            using (var reader = new StreamReader(stream, encoding))
            {
                string line;
                while ((line = await reader.ReadLineAsync()) != null)
                {
                    lines.Add(line);
                }
            }

            return lines.ToArray();
        }

【问题讨论】:

  • 这里为什么需要async?无论哪种方式,您都不会在阅读 .Result...之前等待异步任务完成...
  • (另外,你的数据模型看起来很奇怪——为什么有十几个字符串数组而不是一个包含所有这些字段的结构/类数组?)
  • 你的数据模型看起来很奇怪 - “使用 N 个数组,其中的数据按位置关联”表示程序程序员来到 OO,恕我直言
  • 感谢您的 cmets。首先,我尝试了异步,因为我的其他方法(顺便说一句效果很好)太慢了。这种方法解决了阅读速度慢的问题,但现在我需要弄清楚另一个问题。其次,关于数据模型和其他东西:我是 C# 新手,我正在尝试获得一些经验 - 认为这个项目将是一个很好的机会。现在我有一个我无法解决的问题,我的大脑也纠结于此,所以我不能离开它...... :)
  • @AKX ReadAllLinesAsync 确实在等待,我认为这就足够了。

标签: c# asynchronous readline txt


【解决方案1】:

示例代码有几个问题,我不确定实际问题的原因。

  1. 不要编写自己的 ReadAllLinesAsync,只需使用 File.ReadAllLinesAsync
  2. 您通常应该避免.Result,因为这是一个阻塞操作,这有可能导致死锁。在这种情况下,您应该只调用同步版本 File.ReadAllLines
  3. 如果可能,请使用foreach 或for 循环。它们使您可以更轻松地查看代码是否正确,并避免将循环逻辑分散到多行中。
  4. 据我所知,您对每一行的处理方式相同,因此您应该能够使用 List&lt;string&gt; linesAddRange 合并所有文件中的所有行
  5. 不是保留 15 个不同的属性列表,更常见的方法是存储一个具有 15 个不同属性的类的对象列表。
  6. 当您需要存储数据时,您应该认真考虑使用现有的序列化格式,如 json、xml、csv、protobuf 等。这使您可以使用现有的、经过良好测试的库来写入和读取数据并将其转换为您的自己的类型。

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    相关资源
    最近更新 更多