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

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
Member
 
Join Date: Nov 2005
Posts: 1,323
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
Member
 
Join Date: Oct 2005
Posts: 2,389
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
Member
 
Join Date: May 2008
Posts: 2,381
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
Member
 
Join Date: Feb 2008
Posts: 1,845
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
  #7  
Old 14-05-2012
Member
 
Join Date: May 2012
Posts: 2
star Re: Unable to read Specific Lines of text in c# program

hi every one.....i have one .txt file and i want to read it from perticular line number!

can u please tell me the code
Reply With Quote
  #8  
Old 14-05-2012
Member
 
Join Date: Dec 2007
Posts: 2,256
Re: Unable to read Specific Lines of text in c# program

Quote:
Originally Posted by venkikrazy View Post
hi every one.....i have one .txt file and i want to read it from perticular line number!

can u please tell me the code
Well, you can check out the below code to read a specific txt file from a particular line number:

Code:
 LineIterator it = IOUtils.lineIterator(
        new BufferedReader(FileReader("file.txt")));
  for (int lineNumber = 0; it.hasNext(); lineNumber++) {
     String line = (String) it.next();
     if (lineNumber == expectedLineNumber) {
         return line;
     }
  }
Reply With Quote
  #9  
Old 14-05-2012
Member
 
Join Date: May 2012
Posts: 2
star Re: Unable to read Specific Lines of text in c# program

thak q for the reply but i didnt understand ur code...
Reply With Quote
  #10  
Old 21-05-2012
Member
 
Join Date: May 2012
Posts: 7
Re: Unable to read Specific Lines of text in c# program

I have also the same problem so i have used the library like import using system.io. and take a string of the path of the file.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



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
Need C# script to Read and Write Lines over large database Budhil Software Development 2 25-05-2012 04:54 PM
Any program that can search within 1000 mdi or tiff files for a specific text string? tradertt Software Development 1 07-09-2011 01:46 PM
How to read string from a given text file using C# program? Kasper Software Development 5 25-02-2010 09:18 PM
Unable to read text in Counter-strike Azzan Video Games 3 24-04-2009 01:47 PM
Batch Commands To Read and Delete Lines from txt-File officer07 Windows Server Help 3 04-03-2009 11:49 AM


All times are GMT +5.5. The time now is 07:19 PM.