|
| ||||||||||
| Tags: c sharp, class, for loop, object, program, while loop |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Unable to read Specific Lines of text in c# program
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
| ||||
| ||||
| 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
| ||||
| ||||
| 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];
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| 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 |
|
#5
| ||||
| ||||
| 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
| |||
| |||
| 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:
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 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
| |||
| |||
|
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
| ||||
| ||||
| Re: Unable to read Specific Lines of text in c# program Quote:
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;
}
}
__________________ Education, Career and Job Discussions |
|
#9
| |||
| |||
|
thak q for the reply but i didnt understand ur code... |
|
#10
| |||
| |||
| 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. |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |