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.

This code is more or less based on what Microsoft recommends on their website... any idea on what I'm doing wrong?

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.

1 million line file- OneDrive

有帮助吗?

解决方案

You should not have for inside 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(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.

其他提示

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. Also, your while condition can utilize the file.EndOfStream property.

Basically you are converting every line 1000000 times, because you have the for-loop within your while-loop that does the reading.

Simply remove the for-loop and replace it with 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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top