How to read big text file fast ?

I woulk like to convert this C++ code to C# code :


int cols, rows;
double value;

ifstream file(path);
file >> rows >> cols;
for (int row = 0; row < rows; ++row)
{
   for (int col = 0; col < cols; ++col)
   {
      file >> value;
   }
}


 



This code is used to read Digital Elevation Model, so the text file is often 30mb and can be as big as 1go.

Thanks



Answer this question

How to read big text file fast ?

  • AlexV

    Assuming the data is stored in a binary format, you could use the following to achieve the same result in C#:


    int rows, columns;
    double value;
    BinaryReader
    file = new BinaryReader(new FileStream(path, FileMode.Open));

    rows = file.ReadInt32();
    columns = file.ReadInt32();

    for (int row = 0; row < rows; row++)
    {
       
    for (int column = 0; column < columns; column++)
       {
          value = file.ReadDouble();
       }
    }

    file.Close();


     


    This assumes the file is just a series of doubles, one right after the other. If the DEM file is actually a text file, you can use a TextReader in place of the BinaryReader. Some additional code will be needed to parse the text into doubles (Double.Parse(string)) and handle the specifics of the DEM file format, which I'm not familiar with.

  • How to read big text file fast ?