[英]Reading a file and storing in 2d array on Form Load
I'm designing a simple Epos system as a project and I need to read my stock values in from a file at the beginning of the application in the form of a collection so I am trying to use a 2d int array but not having much luck.我正在设计一个简单的 Epos 系统作为一个项目,我需要在应用程序开始时以集合的形式从文件中读取我的股票值,所以我尝试使用 2d int 数组但运气不佳.
The format of my txt file looks like this:我的 txt 文件的格式如下所示:
15,10,12,19,8 15,10,12,19,8
16,9,11,17,10 16,9,11,17,10
7,6,17,14,11 7,6,17,14,11
8,8,12,13,5 8,8,12,13,5
6,7,13,14,4 6,7,13,14,4
1,4,15,10,10 1,4,15,10,10
6,9,10,14,13 6,9,10,14,13
8,7,9,10,11 8,7,9,10,11
8,12,10,15,6 8,12,10,15,6
9,7,6,13,9 9,7,6,13,9
18,8,7,11,5 18,8,7,11,5
7,12,10,8,9 7,12,10,8,9
12,6,7,9,10 12,6,7,9,10
My code is as follows:我的代码如下:
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");
}
I also have an empty array named Stock declared with the rest of thevariables that I have declared in the method name above.我还有一个名为 Stock 的空数组,它使用我在上面的方法名称中声明的变量的 rest 声明。
int[,] Stock ;
How do I assign my text values to an array so that I can use it later in the application?如何将我的文本值分配给一个数组,以便稍后在应用程序中使用它?
Sorry if I'm not being clear, I'm new to programming.抱歉,如果我不清楚,我是编程新手。 Any help would be greatly appreciated.
任何帮助将不胜感激。 Thanks.
谢谢。
I changed it to use file.readallines as it is what I normally use.我将其更改为使用 file.readallines,因为它是我通常使用的。 I've added an extra array to record all the lines, to then be separated with a split into OneRowOfDataArray.
我添加了一个额外的数组来记录所有行,然后通过拆分为 OneRowOfDataArray 进行分隔。 I added outputs and the line to set the value to the STOCK_ITEMS.
我添加了输出和将值设置为 STOCK_ITEMS 的行。 The only other thing I changed is I removed the spaces in between the rows on the txt file
我唯一改变的另一件事是我删除了 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");
}
}
txt file 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.