You are here:

C#/Reading from a file

Advertisement


Question
QUESTION: Hello Murat

I am learning Visual C# by myself and i realized that is better for me to write my own program and learn from it.
First of all, thanks for your answer but i still i have some doubts. You gave me a link but i could not find my
answer there. This is what i tried:
 
Text.txt:

2 15 18 20 39
7 11 14 15 34
1 31 33 34 50
8 18 45 47 50
1 10 12 32 36
3 4 15 27 37   
1 2 13 19 45
12 17 21 23 30   
11 36 37 41 55   
4 12 13 21 27   

  
private void button1_Click(object sender, EventArgs e)
{          
   string[] lines = File.ReadAllLines(@"C:\Users\Raul\Documents\Text.txt");
         
   // Display the file contents by using a foreach loop.
         
   listBox1.Items.Clear();

   foreach (string line in lines)
   {
      // Use a tab to indent each line of the file.
         
      listBox1.Items.Add("\t\t" + line);
   }          
}  

This work perfect and then i tried this:

private void button1_Click(object sender, EventArgs e)
{          
   string[] lines = File.ReadAllLines(@"C:\Users\Raul\Documents\Text.txt");
   int[,] MyArray = new int[10, 5];

   for (int i = 0; i < lines.Length; i++)
   {
       for (int j = 0; j < 5; j++)
       {
         MyArray[i, j] = Convert.ToInt32(lines[j]);
         listBox1.Items.Add("\t\t" + MyArray);
       }
   }
}

But it does not work, could you help me out please, thanks

ANSWER: Hi back,

It's nice to hear that you have not given up.

I see that you made a great progress. You are trying to fill a multi-dimensional array from file and then show in listbox. As you did at your first method, you can do it directly without using an array, and you can also do it with an array.

First we fill array from the lines that you read from file:

int[,] MyArray = new int[10, 5];

//for every line
for (int i = 0; i < lines.Length; i++)
  {

   //we use split to break the line from space characters.
   string[] numbersInLine = lines[i].Split(' ');
   
      for (int j = 0; j < 5; j++)
      {
        //here we take each number to array
        MyArray[i, j] = Convert.ToInt32(numbersInLine[j]);
      }
  }


Here is the code to fill listbox from the array:

for (int i = 0; i < lines.Length; i++)
  {
   //we use a string to create a line for listbox
   string listBoxLine = "";
   
      for (int j = 0; j < 5; j++)
      {
        //each number in a line is added to the string
        listBoxLine = listBoxLine + MyArray[i, j] + " ";
      }
   //we add line to listbox
   listBox1.Items.Add(listBoxLine);
  }


You can combine these 2 code parts to improve performance:

int[,] MyArray = new int[10, 5];

//for every line
for (int i = 0; i < lines.Length; i++)
  {

   //we use split to break the line from space characters.
   string[] numbersInLine = lines[i].Split(' ');
   
   string listBoxLine = "";

      for (int j = 0; j < 5; j++)
      {
        //here we take each number to array
        MyArray[i, j] = Convert.ToInt32(numbersInLine[j]);

        //each number in a line is added to the string
        listBoxLine = listBoxLine + MyArray[i, j] + " ";         
      }
   listBox1.Items.Add(listBoxLine);
  }


Keep up your good work.

Hope that helps,

Murat

---------- FOLLOW-UP ----------

QUESTION: Hello Murat,
Thanks for answer me too fast, your explanation was detailed and clear thanks again, i ran your code, but it showed an exception that i show you now:

string[] lines = File.ReadAllLines(@"C:\Users\Raul\Documents\Text.txt"); int[,] MyArray = new int[10, 5];

//for every line
for (int i = 0; i < lines.Length; i++)
{

   //we use split to break the line from space characters.
   string[] numbersInLine = lines[i].Split(' ');

   string listBoxLine = "";

   for (int j = 0; j < 5; j++)
   {
       //here we take each number to array

      /* Here i get an exception:
         FormatException was unhandled
         Input string was not in a correct format. */

         MyArray[i, j] = Convert.ToInt32(numbersInLine[j]);

       //each number in a line is added to the string
         listBoxLine = listBoxLine + MyArray[i, j] + " ";
    }
    listBox1.Items.Add(listBoxLine);
}

Could you help, Thanks.

ANSWER: Check your file. You probably have space character in the end of some lines, just delete them and you are ready.

---------- FOLLOW-UP ----------

QUESTION: Hello Murat
I checked my file and also i created another one, but the exception still occurs(I am using to run this program visual c# 2010 express). I ran this program in C++ using the IDE code::block and it works.
Do you thinks the problem is with visual c# 2010 express?
If you have another idea how to fix this exception help me out please, thanks

Answer
Hi again,

Tested the program and approved with the following code:

int[,] MyArray = new int[10, 5];

           //for every line
           string[] lines = System.IO.File.ReadAllLines(@"C:\Text.txt");
           for (int i = 0; i < lines.Length; i++)
           {

               //we use split to break the line from space characters.
               string[] numbersInLine = lines[i].Split(' ');

               string listBoxLine = "";

               for (int j = 0; j < 5; j++)
               {
                   //here we take each number to array
                   MyArray[i, j] = Convert.ToInt32(numbersInLine[j]);

                   //each number in a line is added to the string
                   listBoxLine = listBoxLine + MyArray[i, j] + " ";
               }
               listBox1.Items.Add(listBoxLine);
           }


with the following file:

Text.txt
Note: Wait 30 seconds and click download.

I suppose either your file is not clean or you saved it in unicode or something but the problem is about file.

Hope that helps,

Murat

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Murat Mehmet

Expertise

I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.

Experience

I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Organizations
Was in R & D for 2 years in a popular Turkish technology website: cyber-warrior.com

Education/Credentials
2011 Computer Engineering graduation in University of Trakia in Turkey.

©2016 About.com. All rights reserved.