w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Error in read data from text file and write result in a text file by C#

Console.WriteLine(a) will write your Array's type not the current value to the Console (then to the text file). Instead change your code like this:

   // define i and j here
   int i, j;
   foreach(string value in values)
   {
      i = Convert.ToInt32(values[0]);
      j  = Convert.ToInt32(values[1]);
      a[i, j] = Convert.ToDouble(values[2]);
   }

Then change this line:

Console.WriteLine(a);

To:

Console.WriteLine(a[i, j]);

And instead of creating a new FileStream and StreamWriter, you can do it after your loop finished. First populate your array.Then after the foreach loop:

var allValues = a.OfType<double>();
using(var filestream = new FileStream("1000.txt", FileMode.Create))
using(var streamwriter = new StreamWriter(filestream))
{
    foreach(double value in values)
           streamwriter.WriteLine(value);
}

Here is the full code!

private static void Main(string[] args)
    {
        string line;
        double[,] a = new double[89359, 100];
        StreamReader file = new StreamReader("c:\joao.txt");
        for (int x = 1; x <= 8935900; x++)
        {
            line = file.ReadLine();
            string[] values = line.Split(' ');
            int i, j;
            foreach (string value in values)
            {
                i = Convert.ToInt32(values[0]);
                j = Convert.ToInt32(values[1]);
                a[i, j] = Convert.ToDouble(values[2]);
            }
        }

        var allValues = a.OfType<double>();
        using (var filestream = new FileStream("1000.txt",
FileMode.Create))
        using (var streamwriter = new StreamWriter(filestream))
        {
            foreach (double value in allValues)
                streamwriter.WriteLine(value);
        }
    }




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.