Results 1 to 10 of 10

Thread: Unable to read Specific Lines of text in c# program

  1. #1
    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.

  2. #2
    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();
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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];

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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];

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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]);
                }

  6. #6
    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.

    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.

  7. #7
    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

  8. #8
    Join Date
    Dec 2007
    Posts
    2,291

    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;
         }
      }

  9. #9
    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...

  10. #10
    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.

Similar Threads

  1. Need C# script to Read and Write Lines over large database
    By Budhil in forum Software Development
    Replies: 2
    Last Post: 25-05-2012, 04:54 PM
  2. Replies: 1
    Last Post: 07-09-2011, 01:46 PM
  3. How to read string from a given text file using C# program?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:18 PM
  4. Unable to read text in Counter-strike
    By Azzan in forum Video Games
    Replies: 3
    Last Post: 24-04-2009, 01:47 PM
  5. Batch Commands To Read and Delete Lines from txt-File
    By officer07 in forum Windows Server Help
    Replies: 3
    Last Post: 04-03-2009, 12:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,674,605.23221 seconds with 17 queries