簡體   English   中英

從文本文件填充數組,然后在控制台C#中寫入它們

[英]Filling array from text file and then writing them in console C#

這是我的代碼..練習要求將這些整數寫入文件,然后打開文件,並使用文件中的這些整數填充二維數組,然后在控制台上將其打印出來。

這是我編寫的代碼,在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();
            } 


        }
    }
}

兩次使用的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
  • 文件內容與數組不匹配
    使用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時會出錯。 為了解決這個問題,例如,您可以使用拆分功能
  • 來自msdnFile.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]);
}

我在您的程序中更改了您的一些代碼(注釋將幫助您識別它們)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM