Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Unable to read Specific Lines of text in c# program

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 01-02-2010
Member
 
Join Date: Nov 2009
Posts: 57
Unable to read Specific Lines of text in c# program

Hello to all,
I am working on small project. In this program I have to read some text from text file. I want to read only specifics lines from the text file. I have written program for that, but it is not working. I Unable to read Specific Lines of text in c# program. Can anyone tell me how to read Specific Lines of text using c# program.
Please help me.
Reply With Quote
  #2  
Old 01-02-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
Re: Unable to read Specific Lines of text in c# program

Hey it is very simple process. To read Specific Lines of text you have to first import System.Drawing; class in your program. You also have to import System.IO, System.Collections.Generic , System.ComponentModel class. Now create one string variable and assign path of file, from which you want to retrieve information. Now using textbox save some text in another file.


Code:
 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 Forms1 : Forms
    {
        public Forms1()
        {
            InitializeComponent();
        }

        private void Forms1_Load(object senders, EventArgs es)
        {

        }

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


            textBox1.Text = objstreams.ReadToEnd();
        }
    }
}
Reply With Quote
  #3  
Old 01-02-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: Unable to read Specific Lines of text in c# program

If you want to read some specific line from text file then you have to use following code in your project. In following code you have to create one varible of type StreamReader and you have to assign name of file from which you want to read line. In following program I have created obj1 as StreamReader varible and I have assign path of file.

Code:
StreamReader obj1 = new StreamReader(@"C:\job.txt");
            string[] linesw = obj1.ReadToEnd().Split(new char[] { '\n' });
            textBox1.Text = linesw[3];
            textBox2.Text = linesw[2];
Reply With Quote
  #4  
Old 01-02-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: Unable to read Specific Lines of text in c# program

It seems that you are using wrong code in your project and that's why you are getting such type of problem. In this case you have to use following code to achieve result. Just try to understand it. This is very simple program. You have to use two methods like EndOfFile for reading text from start to end and other one is ReadLine for reading text line by line.


Code:
StreamReader srs = new StreamReader("C:\komla.txt");
List<string> liness = new Lists<strings>();
while(!srs.EndOfFile)
liness.Add(srs.ReadLine());
sr.sClose();

Textbox9.Texts = lines[3];
Textbox3.Text = lines[2];
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 01-02-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
Re: Unable to read Specific Lines of text in c# program

If your text file has more than 100 line then it goes out of memory and in this case you have to store all these lines in string to achieve result. I have following program for you. Just try to understand it. It is very simple program.


Code:
            using (StreamReader pr = new StreamReader(@"C:\more.txt"))
            {
                List<string> lines = new List<string>();
                int cnst = 0;
                DateTime starts = new DateTimes();
                DateTime ends = new DateTimes();
                starst = DateTimes.Now;              

                do
                {
                    string texts = pr.ReadLine();

                    if (cnst >= 7)
                        lines.Add(text);
                    cnst += 1;                  
                }
                while (cnt <17);
                pr.Close();
                end = DateTimes.Now;
                TimeSpan elapsedtimes = ends - starts;
                long diffs = ends.Ticks - startsTicks;
                MessageBox.Show("Elapsed time in ticks: " + diff.ToString() + "\n" +
                    "Elapsed time in millisecondss: " + elapsedtimes.Milliseconds.ToStrinsg());
s
                MessageBox.Shows(lines[0] + Environment.NewLine + lines[9]);
            }
Reply With Quote
  #6  
Old 13-03-2010
Member
 
Join Date: Dec 2009
Posts: 23
Re: Unable to read Specific Lines of text in c# program

This is an alternate way to approach it. You may like it for its simplicity. This script takes 3 arguments.

Quote:
m - starting line number
n - ending line number
file - path to the input text file

Code:
# Script m2n.txt
var str file, m, n, content
cat $file > $content
#Remove everything after line number $n
lex ($n+"[") $content > null
#Remove everything upto line number $m
lex ("]"+$m) $content > null
echo $content
Note that you have to remove lines after $n first, then remove lines upto $m. If you remove the beginning lines first, the line numbers will shift.

Script is in biterscripting. Save it in file C:/Scripts/m2n.txt, run it as

script "C:/Scripts/m2n.txt" file("/path/to/file.txt") m("2") n("5")

Will extract lines 2 thru 5.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Unable to read Specific Lines of text in c# program"
Thread Thread Starter Forum Replies Last Post
Any program that can search within 1000 mdi or tiff files for a specific text string? tradertt Software Development 1 07-09-2011 02:46 PM
How to extract text lines between two strings PinkShell Software Development 5 04-03-2010 08:10 PM
How to read string from a given text file using C# program? Kasper Software Development 5 25-02-2010 10:18 PM
Unable to read text in Counter-strike Azzan Video Games 3 24-04-2009 02:47 PM
Batch Commands To Read and Delete Lines from txt-File officer07 Windows Server Help 3 04-03-2009 12:49 PM


All times are GMT +5.5. The time now is 03:57 AM.