5 Replies - 7826 Views - Last Post: 10 November 2010 - 06:19 AM Rate Topic: -----

#1 Kabanga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 09

How to read file into a two dimensional array

Posted 10 November 2010 - 03:17 AM

Hello!
my file (extension *.z) can be read with notepad and contents some header lines and integer data (70 rows , 9 columns).
I'd like to read that file into a two dimensional array.
Can somebody give me some tips how to do it? I've unsuccessfuly (see the attached file) tried it.
My aim is to skip the header lines, and save the integer data in a array that I can manipulate and display afterwards.

Best regards
Kabanga

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: How to read file into a two dimensional array

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,539
  • Joined: 23-August 08

Re: How to read file into a two dimensional array

Posted 10 November 2010 - 04:43 AM

WHY did you post this information as PDF attachments? Put your code IN your post IN CODE TAGS, as seen below

:code:
Was This Post Helpful? 0
  • +
  • -

#3 Kabanga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 09

Re: How to read file into a two dimensional array

Posted 10 November 2010 - 05:26 AM

here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ReadFileIntoArray
{
    public partial class Form1 : Form
    {

        //declare a two dimensional array which will hold the extracted data
        int[,] extractedData1 = new int[70, 3];
        int[,] extractedData2 = new int[70, 9];
        int[,] extractedData3 = new int[70, 2]; 
        
        
        public Form1()
        {
            InitializeComponent();
        }//end Form1()

        //executed when the "Read data" button is clicked
        private void btnReadData_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                //string data = Read(openFileDialog1.FileName);
                //textBox1.Text = data;

                //read data into an int array
                extractedData2 = System.IO.File.ReadAllLines(@"E:\ReadFileIntoArray\myFile.z");
            }
            else
            {

                //do nothings
            }

        }//end btnReadData_Click(...)

        //method for reading the data
        private string Read(string file)
        {

            StreamReader reader = new StreamReader(file);
            string data = reader.ReadToEnd();
            reader.Close();

            return data;
        }//end Read(...)

    }//end class Form1

}//end namespace






The file "MyFile.pdf" contents only the data to be read and some comments. That's why I sent it as .pdf
I've tried to attach the text file but the system didn't accept it.

Regards
Was This Post Helpful? 0
  • +
  • -

#4 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: How to read file into a two dimensional array

Posted 10 November 2010 - 05:32 AM

Do you store the data in the Z-File? Why you dont use an delemiter? Why you dont use XML?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,539
  • Joined: 23-August 08

Re: How to read file into a two dimensional array

Posted 10 November 2010 - 05:36 AM

This should get you started on reading the text file.

For each line you read you will need to parse the line and insert the values into the array as appropriate.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5566
  • View blog
  • Posts: 11,903
  • Joined: 02-June 10

Re: How to read file into a two dimensional array

Posted 10 November 2010 - 06:19 AM

Writing a text file tutorial.
Reading a text file tutorial.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1