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.
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();
}
}
}
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];
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];
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]);
}
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.
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
Re: Unable to read Specific Lines of text in c# program
Quote:
Originally Posted by
venkikrazy
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;
}
}
Re: Unable to read Specific Lines of text in c# program
thak q for the reply but i didnt understand ur code...
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.