10 Replies - 1425 Views - Last Post: 23 February 2010 - 05:45 AM Rate Topic: -----

#1 deathtoall141   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-February 10

How to read certain lines form a file

Posted 23 February 2010 - 02:31 AM

HI I am trying to make my program to read only certain lines of text but I want this function to be able to be turned off or on. So a user can read both the full document and a summarized version.

Here you can see my attempt but it doesnt have the desired effect. Any help would be most appreciated.

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 filestream
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string Read(string file)
{

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

return data;

}
private void btn1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string data = Read(openFileDialog1.FileName);
textBox1.Text = data;

}
else
{

//do nothing
}
}

private void button1_Click(object sender, EventArgs e)
{

int NumberOfLines = 15;
string[] ListLines = new string[NumberOfLines];

for (int i = 1; i < NumberOfLines; i++)

Console.WriteLine(ListLines[5]);
Console.WriteLine(ListLines[1]);

}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: How to read certain lines form a file

#2 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to read certain lines form a file

Posted 23 February 2010 - 02:51 AM

Sorry but it is not easy to read code formatted so. Please :code: Once that is taken care of then I will look it over and see what I can do about the problem. Don't mean to be that way about it, but it is seriously not fun to read.
Was This Post Helpful? 0
  • +
  • -

#3 deathtoall141   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-February 10

Re: How to read certain lines form a file

Posted 23 February 2010 - 02:55 AM

Hey sorry about that I didnt realize.

 
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 filestream
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string Read(string file)
        {

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

            return data;

        }
        private void btn1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string data = Read(openFileDialog1.FileName);
                textBox1.Text = data;

            }
            else
            {

                //do nothing
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int NumberOfLines = 15;
            string[] ListLines = new string[NumberOfLines];

            for (int i = 1; i < NumberOfLines; i++)

                Console.WriteLine(ListLines[5]);
                Console.WriteLine(ListLines[1]);

        }
    }
}



Hope thats better.
Was This Post Helpful? 0
  • +
  • -

#4 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to read certain lines form a file

Posted 23 February 2010 - 03:02 AM

First off some syntax things to fix
private void button1_Click(object sender, EventArgs e)
        {

            int NumberOfLines = 15;
            string[] ListLines = new string[NumberOfLines];

            for (int i = 1; i < NumberOfLines; i++)
            { // You forgot the brackets around the body of the for loop
                Console.WriteLine(ListLines[5]); // ListLines is an array 15 elements in length with nothing in them.  
                Console.WriteLine(ListLines[1]); // You need to put values in the array before using it.
            } // You forgot the brackets around the body of the for loop
        }



Was This Post Helpful? 0
  • +
  • -

#5 Ferencn   User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: How to read certain lines form a file

Posted 23 February 2010 - 03:16 AM

private void button1_Click(object sender, EventArgs e)
        {

            int NumberOfLines = 15;
            string[] ListLines = new string[NumberOfLines];

            for (int i = 1; i < NumberOfLines; i++)
            { // You forgot the brackets around the body of the for loop
                Console.WriteLine(ListLines[5]); // ListLines is an array 15 elements in length with nothing in them.  
                Console.WriteLine(ListLines[1]); // You need to put values in the array before using it.
            } // You forgot the brackets around the body of the for loop
        }



You also don't assign anything to your array of strings.
Instead of reading the file as a whole, you could try to read the file line by line, using ReadLine(); You can find more about this method, and an example here:
ReadLine
Also be careful not to read more than 15 lines, if you use your current Array.
EDIT:
You could replace your fixedsize array with an ArrayList so you are not limited to those 15 lines.

This post has been edited by Ferencn: 23 February 2010 - 03:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 deathtoall141   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-February 10

Re: How to read certain lines form a file

Posted 23 February 2010 - 03:48 AM

Hey thanks for your help, but im more confused now then ever. I'm really new to programming and trying to learn through tutorials. I'll see if i can find a turtorial that will explain it for idiots.
Was This Post Helpful? 0
  • +
  • -

#7 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to read certain lines form a file

Posted 23 February 2010 - 04:03 AM

Here is a basic run down for you:
int NumberOfLines = 15;
string[] ListLines = new string[NumberOfLines];


These two lines create an int variable called NumberOfLines that has a value of 15 and a string array called ListLines that has enough room for 15 strings. You don't actually put anything into those 15 places, but they are there and waiting.
for (int i = 1; i < NumberOfLines; i++)


This line creates an int variable called i that has a starting value of one. It then runs whatever is inside the body of this loop as long as the expression i < NumberOfLines; is true. A good side note here is that all array's start at zero so you really want your variable of i (the counter variable) to also start at zero or else you will skip the first element every time. i++ simply increases the value of i by 1 each time the loop finishes. After that code should come a pair of curly brackets that contain all the code that is to be run for each iteration of the loop.
Console.WriteLine(ListLines[5]);
Console.WriteLine(ListLines[1]);


These two lines of code here print out the elements at positions number 5 and 1 in the ListLines array. Again since you didn't put any values into this array there is nothing here to retrieve. I think a better option (going on the assumption that you want to print every item in the array) would be to replace these two lines of code with the following ONE line of code
Console.WriteLine(ListLines[i]);


That will allow your loop to use the counter variable to go through the array that was created and print each element to the array. As to what you want in the variable I can't even wager a guess so I can't offer much insight into how to properly implement that.

This post has been edited by Adkins: 23 February 2010 - 05:45 AM

Was This Post Helpful? 2
  • +
  • -

#8 deathtoall141   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-February 10

Re: How to read certain lines form a file

Posted 23 February 2010 - 04:09 AM

Thanks thats exactly what ive been looking for.
Was This Post Helpful? 0
  • +
  • -

#9 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to read certain lines form a file

Posted 23 February 2010 - 04:21 AM

no worries and glad I could help
Was This Post Helpful? 0
  • +
  • -

#10 Ferencn   User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: How to read certain lines form a file

Posted 23 February 2010 - 05:18 AM

View PostAdkins, on 23 February 2010 - 03:03 AM, said:

A good side note here is that all array's start at zero so you really want your variable of i (the counter variable) to also start at one or else you will skip the first element every time.

Overall a good explanation! I think you mean to say "also start at zero".
Was This Post Helpful? 1
  • +
  • -

#11 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to read certain lines form a file

Posted 23 February 2010 - 05:45 AM

You sir are correct. The change has been made.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1