none
Visual C# reading from file. RRS feed

  • Question

  • I'm using VS 2017 C# windows forms.I have a 2D array:

    string[,] bigData = new string[32,25];

    I'm trying to read value form txt for the array:

                string input = File.ReadAllText(@"c:\bigData.txt");
                int i = 1;
                int j = 1;
                foreach(var row in input.Split('\n'))
                {
                    j = 1;
                    foreach(var col in row.Trim().Split(' '))
                    {
                        bigData[i,j] = col.Trim();
                        j++;
                    }
                    i++;
                }

    (the bigdata.txt is filled with numbers)

    something is bad,after reading the array is full of 0.

    What is wrong?


    • Edited by Big Johny Friday, December 21, 2018 10:52 AM
    • Moved by May Luo-MSFT Monday, December 24, 2018 2:44 AM c# issue
    Friday, December 21, 2018 10:52 AM

All replies

  • Hi Big Johny,

    Thank you for posting here.

    For your question, you could try the code below to read data from file and save it to 2D array.

    string[,] bigData = new string[32, 25];
    
                // I'm trying to read value form txt for the array:
    
                var input = File.ReadAllLines("bigData.txt");
    
                foreach (var row in input)
                {
                    var s = row.Split(' ');
                    for (int i = 0; i < bigData.GetLength(0); i++)
                    {
                        for (int j = 0; j < bigData.GetLength(1); j++)
                        {
                            bigData[i, j] = s[j];
                        }
                    }
                }

    My .txt file.


    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 2:14 AM
    Moderator
  • Hi Big Johny,

    Welcome to the MSDN forum.

    Based on your question, it seems to be related to C# development. Since our forum mainly focus on VS IDE issue. I will help you move it to the appropriate forum for a more professional supporting.

    Thank you for your understanding,

    Sincerely.

    May


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, December 24, 2018 2:44 AM
  • How do you know it is full of zeros?  Did you look at the whole array, or only the first row?  "0" doesn't make sense, because if you hadn't read anything, the array should contain empty strings.

    The only thing fundamentally wrong with your code is that you start filling with element 1, whereas C# arrays start numbering at 0.  Thus, bigData[0][x] will be empty for all values of x.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, December 24, 2018 2:49 AM