C# 读取文件并在表单加载时以2d数组存储
我正在设计一个简单的Epos系统作为一个项目,我需要以集合的形式从应用程序开始时的文件中读取我的股票价值,所以我尝试使用2d int数组,但运气不太好 我的txt文件的格式如下所示: 15,10,12,19,8 16,9,11,17,10 7,6,17,14,11 8,8,12,13,5 6,7,13,14,4 1,4,15,10,10 6,9,10,14,13 8,7,9,10,11 8,12,10,15,6 9,7,6,13,9 18,8,7,11,5 7,12,10,8,9 12,6,7,9,10 我的代码如下:C# 读取文件并在表单加载时以2d数组存储,c#,arrays,file-io,C#,Arrays,File Io,我正在设计一个简单的Epos系统作为一个项目,我需要以集合的形式从应用程序开始时的文件中读取我的股票价值,所以我尝试使用2d int数组,但运气不太好 我的txt文件的格式如下所示: 15,10,12,19,8 16,9,11,17,10 7,6,17,14,11 8,8,12,13,5 6,7,13,14,4 1,4,15,10,10 6,9,10,14,13 8,7,9,10,11 8,12,10,15,6 9,7,6,13,9 18,8,7,11,5 7,12,10,8,9 12,6,7,
private void ReadToFileOpeningStock(int [,] Stock)
{
//create an array to hold data from file
string[] OneRowOfDataArray;
const int StockColumns = 13;
const int StockRows = 5;
int[,] STOCK_ITEMS = new int[StockColumns, StockRows];
try
{
// Declare a StreamReader variable.
StreamReader inputFile;
// Open the file and get a StreamReader object.
inputFile = File.OpenText("Opening StartingStock.txt");
while (!inputFile.EndOfStream)
{
OneRowOfDataArray = inputFile.ReadLine().Split(',');
for (int i = 0; i < StockColumns; i++)
{
//Here are the inner columns
for (int j = 0; j < StockRows; j++)
{
}
}
}
inputFile.Close();
}
catch
{
MessageBox.Show("Error");
}
如何将文本值分配给数组,以便以后在应用程序中使用
对不起,如果我不清楚,我是编程新手。任何帮助都将不胜感激。谢谢。我将它改为使用file.readallines,因为它是我通常使用的。我添加了一个额外的数组来记录所有的行,然后通过拆分为OneRowOfDataArray来分隔这些行。 我添加了输出和行来设置STOCK_项的值。我唯一改变的另一件事是删除了txt文件行之间的空格
static int[,] STOCK_ITEMS = new int[4, 3];
static void Main(string[] args)
{
//create an array to hold data from file
string[] RowsOfData;//contains rows of data
string[] OneRowOfDataArray;//will contain values seperated by the rows
const int StockColumns = 13;
const int StockRows = 5;
int[,] STOCK_ITEMS = new int[StockColumns, StockRows];
try
{
// Open the file and get a StreamReader object.
RowsOfData = File.ReadAllLines("Opening StartingStock.txt");//sets all lines and seperates them into ROWSOFDATA array
for (int i = 0; i < StockColumns; i++)
{
OneRowOfDataArray = RowsOfData[i].Split(',');//splits the values in each row seperate
Console.WriteLine();//new line when outputting the data
//Here are the inner columns
for (int j = 0; j < StockRows; j++)
{
STOCK_ITEMS[i, j] = Int32.Parse(OneRowOfDataArray[j]);//save to correct index in stock items
Console.Write("[" + STOCK_ITEMS[i, j] + "]");//output value from the row
}
}
}
catch
{
MessageBox.Show("Error");
}
}
好的,我想我明白了,谢谢。我是否只是将Main(string[]args)放在表单加载事件中,以便它将值分配给全局变量数组?不,我有Main,就像我在控制台窗口中做的那样。main方法中的代码可以放入on-form-load方法中。这是一种处理方法。谢谢你的帮助!
static int[,] STOCK_ITEMS = new int[4, 3];
static void Main(string[] args)
{
//create an array to hold data from file
string[] RowsOfData;//contains rows of data
string[] OneRowOfDataArray;//will contain values seperated by the rows
const int StockColumns = 13;
const int StockRows = 5;
int[,] STOCK_ITEMS = new int[StockColumns, StockRows];
try
{
// Open the file and get a StreamReader object.
RowsOfData = File.ReadAllLines("Opening StartingStock.txt");//sets all lines and seperates them into ROWSOFDATA array
for (int i = 0; i < StockColumns; i++)
{
OneRowOfDataArray = RowsOfData[i].Split(',');//splits the values in each row seperate
Console.WriteLine();//new line when outputting the data
//Here are the inner columns
for (int j = 0; j < StockRows; j++)
{
STOCK_ITEMS[i, j] = Int32.Parse(OneRowOfDataArray[j]);//save to correct index in stock items
Console.Write("[" + STOCK_ITEMS[i, j] + "]");//output value from the row
}
}
}
catch
{
MessageBox.Show("Error");
}
}
15,10,12,19,8
16,9,11,17,10
7,6,17,14,11
8,8,12,13,5
6,7,13,14,4
1,4,15,10,10
6,9,10,14,13
8,7,9,10,11
8,12,10,15,6
9,7,6,13,9
18,8,7,11,5
7,12,10,8,9
12,6,7,9,10