繁体   English   中英

StreamReader不会停止读取文本文件

[英]StreamReader doesn't stop reading text file

我有一个程序可以读取一百万行的文件。 每行上都有一个浮点值。 该值将被读取并放入数组的元素中。

using System;
using System.Diagnostics;
using System.IO;

namespace sort1mRandFloat
{
    public class Program
    {
        static void Main()
        {
            Console.WriteLine("Creating Single array...");
            Single[] fltArray = new Single[1000000];

            Console.WriteLine("Array created, making string...");
            String line;
            Console.WriteLine("String created, opening file...");
            StreamReader file = new StreamReader(@"C:\\Users\\Aaron\\Desktop\\rand1mFloats.txt");
            Console.WriteLine("File opened, creating stopwatch and starting main execution event. See you on the other side.");
            int i;
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while((line = file.ReadLine()) != null)
            {
                for(i=0; i < 1000000; i++)
                {
                    fltArray[i] = Convert.ToSingle(line);
                    if (i == 999999)
                        Console.WriteLine("At 999999");
                }
            }

            file.Close();
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;
            String elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                    ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds/10);
            Console.WriteLine("It took " + elapsedTime + " to read a thousand lines into the array.\n");
            Console.WriteLine("Element 0 is: " + fltArray[0]);
            Console.WriteLine("Element 999999 is: " + fltArray[999999]);          
            Console.ReadLine();
        }
    }
}

在文件上运行此代码时,它永远不会停止。 它正在寻找某种东西来告诉它它在瓷砖或其他东西的末端,而找不到它。 填充第999,999个元素后,它会循环回到0,然后重新开始。

这段代码或多或少地基于Microsoft在其网站上的建议...对我做错的事情有任何想法吗?

该文件可以在下面找到。 由于我还不能将文件存储在阵列中,因此我无法说清楚它需要多长时间才能工作。 文件中有很多值。 计量连接警告:18 MB文件。

一百万行文件-OneDrive

你不应该for内部while 您只需要一个循环:

var i = 0;
while((line = file.ReadLine()) != null)
{
    fltArray[i] = Convert.ToSingle(line);
    if (i == 999999)
        Console.WriteLine("At 999999");
    i++;
}

for

for(i=0; i < 1000000 && (line = file.ReadLine()) != null; i++)
{
    fltArray[i] = Convert.ToSingle(line);
    if (i == 999999)
        Console.WriteLine("At 999999");
}

更新资料

我正在为您的文件获得以下结果:

Creating Single array...
Array created, making string...
String created, opening file...
File opened, creating stopwatch and starting main execution event. See you on the other side.
At 999999
It took 00:00:00.42 to read a thousand lines into the array.

Element 0 is: 0,9976465
Element 999999 is: 0,04730097

发布版本,运行在VS之外,i5-3317U @ 1.7GHz。

我正在用手机打电话,为简洁起见,我深表歉意。 您的外部while循环将遍历您的100万行,而内部的for循环将迭代1百万次,总共进行1万亿次迭代。 另外,您的while条件可以利用file.EndOfStream属性。

基本上,您将每行转换1000000次,因为您的while循环中有for循环来进行读取。

只需删除for循环并将其替换为i ++

每次调用file.ReadLine时,它都会从文件中读取一行,直到到达文件末尾并变为null(因此退出while循环)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM