簡體   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