Reading Specific Lines of text

How to read specific lines of text from a text file.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 9265 Views - Last Post: 01 February 2010 - 05:02 AM Rate Topic: -----

#1 Zdrenka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 116
  • Joined: 24-November 09

Reading Specific Lines of text

Posted 30 January 2010 - 11:15 AM

Hi, I'm currently writing a small program that will read from a text file, the problem is I need to read only specifics lines, so for example the text file has 15 lines of text and I need to read and display lines 8 and 2 into to text boxes,

so far i have this code which just reads from the whole document.


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

namespace reading
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void btngettxt_Click(object sender, EventArgs e)
		{
			StreamReader objstream = new StreamReader("C:\\Documents and Settings\\Karl\\Desktop\\README.txt");


			textBox1.Text = objstream.ReadToEnd();
		}
	}
}



Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Specific Lines of text

#2 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Reading Specific Lines of text

Posted 30 January 2010 - 12:17 PM

Try something like this
			StreamReader objstream = new StreamReader(@"C:\MyFile.txt");
			string[] lines = objstream.ReadToEnd().Split(new char[] { '\n' });
			textBox1.Text = lines[1];
			textBox2.Text = lines[7];



~'J'~
Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 02:25 AM

Fixo's solution looks cool, but I was wondering, would there be a problem if the lines are splitted with diffferent delimiter (for example with \r\n)???
Was This Post Helpful? 0
  • +
  • -

#4 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 03:48 AM

View PostFlashM, on 31 Jan, 2010 - 01:25 AM, said:

Fixo's solution looks cool, but I was wondering, would there be a problem if the lines are splitted with diffferent delimiter (for example with \r\n)???


Hello FlashM

I'm pretty sure you have an expert solution :)

Please, share it with us :)

~'J'~
Was This Post Helpful? 0
  • +
  • -

#5 remorseless  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 03:56 AM

StreamReader sr = new StreamReader("C:\MyFile.txt");
List<string> lines = new List<string>();
while(!sr.EndOfFile)
lines.Add(sr.ReadLine());
sr.Close();

Textbox1.Text = lines[8]; //Gets the 9th line, remember we always start counting from 0.
Textbox2.Text = lines[0]; //Gets the first line.


This post has been edited by remorseless: 31 January 2010 - 03:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 04:17 AM

remorseless, I was thinking of this solution too. But if your text file has like thousands of lines or even millions, you would eventually run out of memory, trying to fill each and every line in a list of strings. If this would be the case, I would try to read text file line by line, incrementing the counter and if the counter equals the line we want, we would show it in a textbox.

This post has been edited by FlashM: 31 January 2010 - 04:18 AM

Was This Post Helpful? 1
  • +
  • -

#7 Zdrenka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 116
  • Joined: 24-November 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 05:41 AM

That's a good point, the text files I'm reading from have about 23900 lines so it would use alot of memory...

Thanks you guys for the solutions they really helped, I'm well on my way now, but I was wondering...does anyone know how to read from a certain section of a text file? so like read from line 8 to line 16?
I cant seem to find anywhere with an explanation
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 06:45 AM

Read a text file line by line using a StreamReader. Increment counter variable for each line that was read. Then if counter is more or equal to 8 and less then 17, fill those lines into a list of strings.
Was This Post Helpful? 0
  • +
  • -

#9 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Reading Specific Lines of text

Posted 31 January 2010 - 07:33 AM

To answer your question, No. There isn't a way to explicitly say: 'read lines 2 and 17 of the text file'.

You have to use FlashM's suggestion.
Was This Post Helpful? 0
  • +
  • -

#10 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 09:11 AM

View PostZdrenka, on 31 Jan, 2010 - 04:41 AM, said:

That's a good point, the text files I'm reading from have about 23900 lines so it would use alot of memory...

Thanks you guys for the solutions they really helped, I'm well on my way now, but I was wondering...does anyone know how to read from a certain section of a text file? so like read from line 8 to line 16?
I cant seem to find anywhere with an explanation
Thanks

This one will do it:
			using (StreamReader sr = new StreamReader(@"C:\sort.txt"))
			{
				List<string> lines = new List<string>();
				int cnt = 0;
				do
				{
					string text = sr.ReadLine();
					if (cnt >= 7)
						lines.Add(text);
					cnt += 1;
				}
				while (cnt < 17);
				sr.Close();
				Textbox1.Text = lines[0]; //8th line of file
				Textbox2.Text = lines[9]; //17th line of file
				
			}


~'J'~
Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Reading Specific Lines of text

Posted 31 January 2010 - 09:34 AM

If your text file is big you're in for a long wait (relatively). Just a heads up! :P
Was This Post Helpful? 0
  • +
  • -

#12 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 12:12 PM

View Poststapia.gutierrez, on 31 Jan, 2010 - 08:34 AM, said:

If your text file is big you're in for a long wait (relatively). Just a heads up! :P


Sorry I have disagree
I have tested this code on localldd.xt (48.6 Mb) -
geo data of pipe-lines of Mexico Gulf
Try this out:
			//loop through 8th to 17th line of text //OK
			using (StreamReader sr = new StreamReader(@"C:\localldd.txt"))
			{
				List<string> lines = new List<string>();
				int cnt = 0;
				DateTime start = new DateTime();
				DateTime end = new DateTime();
				start = DateTime.Now;			  

				do
				{
					string text = sr.ReadLine();

					if (cnt >= 7)
						lines.Add(text);
					cnt += 1;				  
				}
				while (cnt <17);
				sr.Close();
				end = DateTime.Now;
				TimeSpan elapsedtime = end - start;
				long diff = end.Ticks - start.Ticks;
				MessageBox.Show("Elapsed time in ticks: " + diff.ToString() + "\n" +
					"Elapsed time in milliseconds: " + elapsedtime.Milliseconds.ToString());

				MessageBox.Show(lines[0] + Environment.NewLine + lines[9]);
			}



~'J'~
Was This Post Helpful? 0
  • +
  • -

#13 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Reading Specific Lines of text

Posted 31 January 2010 - 01:05 PM

I meant that if he wanted line 700 of a text file, he would have to wait until it iterates there. :P
Was This Post Helpful? 0
  • +
  • -

#14 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 01:20 PM

View Poststapia.gutierrez, on 31 Jan, 2010 - 12:05 PM, said:

I meant that if he wanted line 700 of a text file, he would have to wait until it iterates there. :P

Ok, nevermind :)

~'J'~
Was This Post Helpful? 0
  • +
  • -

#15 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Reading Specific Lines of text

Posted 31 January 2010 - 02:11 PM

This long running processes can be executed in another thread to achieve a better responsiveness of your windows app.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2