从文本文件读取数据并在C#中将结果写入文本文件时出错

时间:2014-02-14 23:51:11

标签: c#

我想读取一个文本文件,其中有8935900行(如下一行),逐行并将数据放入矩阵(多维数组)并将其保存在文本文件中,因为第一列是(i )和第二列是(j)列(z [i] [j]),第三列显示数组中此位置的值。例如z[1][0] = 0.123413

1 0 0.123413
1 1 0.23423413
1 2 0.2234413
2 0 0.7456413
2 1 0.987651
2 2 0.0358413
3 0 0.4876513
3 1 0.986443
3 2 0.3465413

我写了一个脚本,我有两个问题:

1-代码的逻辑是否正确?

2-如果是正确的话;它显示错误"The process cannot access the file 'C:\\----1000.txt' because it is being used by another process" -IO exception was unhandled

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace _2test
{
    class Program
    {
        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=0;
                int j=0;
                foreach(string value in values)
                {
                    i = Convert.ToInt32(values[0]);
                    j = Convert.ToInt32(values[1]);
                    a[i, j] = Convert.ToDouble(values[2]);
                }

                var valuess = 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);
                }
            }


        }
    }
}

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

Console.WriteLine(a)会将您的Array类型而非当前值写入Console(然后再写入文本文件)。而是改变你的代码:

   // 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]);
   }

然后改变这一行:

Console.WriteLine(a);

要:

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

而不是创建新的FileStreamStreamWriter,您可以在循环结束后执行此操作。首先填充你的数组。然后在foreach循环之后:

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

这是完整的代码!

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