简体   繁体   English

StreamReader不会停止读取文本文件

[英]StreamReader doesn't stop reading text file

I have a program to read a million- line file. 我有一个程序可以读取一百万行的文件。 Each line has one floating-point value on it. 每行上都有一个浮点值。 The value is to be read in and put in an element in an array. 该值将被读取并放入数组的元素中。

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();
        }
    }
}

When this code is run on the file, it doesn't ever stop. 在文件上运行此代码时,它永远不会停止。 It's looking for something to tell it that it's at the end of the tile or something, and it's not finding it. 它正在寻找某种东西来告诉它它在瓷砖或其他东西的末端,而找不到它。 Upon filling the 999,999th element, it loops back to 0 and starts again. 填充第999,999个元素后,它会循环回到0,然后重新开始。

This code is more or less based on what Microsoft recommends on their website... any idea on what I'm doing wrong? 这段代码或多或少地基于Microsoft在其网站上的建议...对我做错的事情有任何想法吗?

The file can be found below. 该文件可以在下面找到。 As I have not been able to store the file in the array yet, I cannot say how long it will take for it to work. 由于我还不能将文件存储在阵列中,因此我无法说清楚它需要多长时间才能工作。 There's quite a few values in the file. 文件中有很多值。 Metered connection warning: 18 MB file. 计量连接警告:18 MB文件。

1 million line file- OneDrive 一百万行文件-OneDrive

You should not have for inside while . 你不应该for内部while You only need one loop: 您只需要一个循环:

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

or with for : for

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

Update 更新资料

I'm getting following results for your file: 我正在为您的文件获得以下结果:

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

Release build, run outside of VS, i5-3317U @ 1.7GHz. 发布版本,运行在VS之外,i5-3317U @ 1.7GHz。

I'm on my phone, so I apologize for the brevity. 我正在用手机打电话,为简洁起见,我深表歉意。 Your outer while loop will hit each of your 1 million lines, and your inner for loop is iterating 1 million times for a total of 1 trillion iterations. 您的外部while循环将遍历您的100万行,而内部的for循环将迭代1百万次,总共进行1万亿次迭代。 Also, your while condition can utilize the file.EndOfStream property. 另外,您的while条件可以利用file.EndOfStream属性。

Basically you are converting every line 1000000 times, because you have the for-loop within your while-loop that does the reading. 基本上,您将每行转换1000000次,因为您的while循环中有for循环来进行读取。

Simply remove the for-loop and replace it with i++ 只需删除for循环并将其替换为i ++

Every time file.ReadLine is called it reads a single line from file until it reaches the end of the file and become null (therefor exiting your while-loop). 每次调用file.ReadLine时,它都会从文件中读取一行,直到到达文件末尾并变为null(因此退出while循环)。

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

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