简体   繁体   中英

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.

The format of my txt file looks like this:

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

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.

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. I've added an extra array to record all the lines, to then be separated with a split into OneRowOfDataArray. I added outputs and the line to set the value to the STOCK_ITEMS. The only other thing I changed is I removed the spaces in between the rows on the txt file

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM