从文本文件填充数组,然后在控制台C#中编写它们

时间:2013-12-16 12:52:17

标签: c# visual-studio-2010

这是我的代码.. 运动要求将这些整数写入文件,然后打开文件并用文件中的那些整数填充二维数组,然后将其打印在控制台上。

这是我编写的代码,在Visual Studio 2010中运行后,它给了我一些奇怪的错误......你能帮我解决一下代码中的问题吗?

这是我写的代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("C:\\Users\\Guest\\Desktop\\hi.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            string filepath = "C:\\Users\\Guest\\Desktop\\hi.txt";

            sw.WriteLine("6,73,6,71");
            sw.WriteLine("32,1,0,12");
            sw.WriteLine("3,11,1,134");
            sw.WriteLine("43,15,43,6");
            sw.WriteLine("55,0,4,12");
            sw.Close();

            StreamReader sr = new StreamReader(fs);
            int [,] data = new int[4,5];
            using (StreamReader reader = File.OpenText(filepath))
            {
                for (int r = 0; r < data.GetLength(0); r++)
                {
                    for (int c = 0; c < data.GetLength(1); c++)
                    {
                      /* 
                        if (reader.EndOfStream)
                        {
                            return ricxvebi;
                        } 
                       */
                             data[r, c] = int.Parse(reader.ReadLine());
                    }
                }
            }

            var rowCount = data.GetLength(0);
            var colCount = data.GetLength(1);
            for (int row = 0; row < rowCount; row++)
            {
                for (int col = 0; col < colCount; col++)
                    Console.Write(String.Format("{0}\t", data[row, col]));
                Console.WriteLine();
            } 


        }
    }
}

3 个答案:

答案 0 :(得分:1)

您使用的Filestream对象在两次出现时都是相同的。在这两种情况下,它都试图创建文件,因为FileMode.Create。尝试单独分解,例如。

FileStream fs;
fs = new FileStream(filepath, FileMode.Create);

//Code to write to and close file

fs = new FileStream(filepath, FileMode.Open);

//code to read file to output

答案 1 :(得分:1)

  • 您的文件内容与您的数组不符 使用int [,] data = new int[5,4];代替int [,] data = new int[4,5];
  • data[r, c] = int.Parse(reader.ReadLine()); ReadLine返回所有行,即6,73,6,71,因此当您尝试将其解析为int时,您会收到错误。例如,为了解决这个问题,您可以使用Split函数
  • 来自msdn
  • File.OpenText(filepath)等同于StreamReader(String)构造函数重载。
  • StreamReaderStreamWriter都有带参数string path的重载构造函数,因此您无需立即手动创建新的文件流。你可以尝试这样的事情

string filepath = "C:\\Users\\Guest\\Desktop\\hi.txt";  
using(StreamWriter sw = new StreamWriter(filepath)){
        .... //your actions
}
....
using(StreamReader sw = new StreamReader(filepath)){
        .... //your actions
}

<强>更新

使用Split函数的示例

var numbersInString = reader.ReadLine().Split(',');
for(int i = 0; i< numbersInString.Length;i++){
    var num = int.Parse(numbersInString[i]);
}

答案 2 :(得分:1)

我在您的程序中更改了几个代码(注释可以帮助您识别它们)

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

namespace ConsoleApplication1
{
   class Program
    { 
        string filepath = "C:\\Users\\Guest\\Desktop\\hi.txt"; // use these as common
        FileStream fs = new FileStream(filepath,FileMode.Create);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine("6,73,6,71");
        sw.WriteLine("32,1,0,12");
        sw.WriteLine("3,11,1,134");
        sw.WriteLine("43,15,43,6");
        sw.WriteLine("55,0,4,12");
        sw.Close();
        string buffer = "";
        FileStream fs1 = new FileStream(filepath, FileMode.Open); // changes are here in FileMode
        StreamReader sr = new StreamReader(fs1);
        int[,] data = new int[5, 4]; // your array index is short
        int i = 0, j = 0;
        while ((buffer = sr.ReadLine()) != null)
        {
            var row = buffer.Split(',');
            foreach (var rowItem in row)
            {
                data[i, j] = Convert.ToInt32(rowItem); 
                j++;
            }
            i++; j = 0;
        }
    }
}