473,508 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read line in textfile

Hello.

Is there a way to read a specified line in a textfile?
Something like TextReader.ReadTextLine(4);

It seems very odd to use
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
string wishedLine = reader.ReadLine();

Is there a better way?
Greetings and thanks in advance,
Tim.
Nov 16 '05 #1
2 9939
Tim Bücker <ti*************@web.de> wrote:
Is there a way to read a specified line in a textfile?
Something like TextReader.ReadTextLine(4);

It seems very odd to use
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
string wishedLine = reader.ReadLine();

Is there a better way?


Well, you could write:

void SkipLines (TextReader reader, int lines)
{
for (int i=0; i < lines; i++)
{
reader.ReadLine();
}
}

and then do:

SkipLines (reader, 3);
string line = reader.ReadLine();

There's no other way to do it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Or if you require random access to the lines, you should read the lines into
an ArrayList and then access them using an index.

ArrayList lines = new ArrayList();
string line;

while ((line = reader.ReadLine()) != null)
lines.Add(line);

string wishedLine = (string) lines[3];

HTH,
Stefan

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tim Bücker <ti*************@web.de> wrote:
Is there a way to read a specified line in a textfile?
Something like TextReader.ReadTextLine(4);

It seems very odd to use
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
string wishedLine = reader.ReadLine();

Is there a better way?


Well, you could write:

void SkipLines (TextReader reader, int lines)
{
for (int i=0; i < lines; i++)
{
reader.ReadLine();
}
}

and then do:

SkipLines (reader, 3);
string line = reader.ReadLine();

There's no other way to do it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2005
by: dmiller23462 | last post by:
Hi again guys.... This is my first source of help, by far the best ASP intellectual collective....Thanks in advance for any assistance...Anyway.... The forms I'm still working on (finishing...
6
2659
by: eight02645999 | last post by:
hi wish to ask a qns on strip i wish to strip all spaces in front of a line (in text file) f = open("textfile","rU") while (1): line = f.readline().strip() if line == '': break print line
9
3451
by: Justme | last post by:
Novice programmer needs help with using fgets to read and ignore the first two lines of a file. I've gone thru the previous posting regarding fgets, but none of them seems to help my situation. I...
8
6273
friendjin
by: friendjin | last post by:
How can I read every line from the existed file. eg. the content of textfile: serialnumber = " 50"; serialfile path = "C:\\doc\\"; errorfile path = "C:\\doc\\d\\"; I want to read "50" from...
6
3539
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
1
2044
by: saytri | last post by:
Hi i'm a student studying java (i'm still a beginner). i have a project were i have to do a quiz with a set of questions. So i think that i have to type the questions in a textfile for example in...
0
1380
by: Snoze | last post by:
Hello, I'm trying to make an application with C# that writes and reads some lines of a text file, but I want to read one specific line and save the text in a string variable. And the inverse...
6
7056
by: Per Juul Larsen | last post by:
Hi. My Textfile looks like this Variable 1 Variable 2 Variable 3 Variable 4 On opening the application it will read the textfile, and assign each line of variable to at textfield in VB and...
3
1578
by: Raymond Chiu | last post by:
Dear all, Now I have the simple task to read text file line by line into database (SQL Server 2000). If the text file is divided into different fields according to the number of characters,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.