|
-
Apr 8th, 2006, 09:38 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2.0] BaseStream Skip Line
Wow.. I feel soo stupid that I can't fiqure this out. I am reading a file and I want to read the second line. Is there a way to just skip the line? Thanks
And also, to read that line? For example:
File contains:
### Initials ###
GAP
I want to read GAP and put it in a variable
Last edited by Fromethius; Apr 8th, 2006 at 09:48 PM.
-
Apr 8th, 2006, 10:27 PM
#2
Re: [2.0] BaseStream Skip Line
There is no such thing as skipping a line when reading a text file. StreamReaders are sequential and forward-only. If you want a specific line then simply read all the lines up to that one and discard them.
-
Apr 8th, 2006, 10:31 PM
#3
Thread Starter
Frenzied Member
Re: [2.0] BaseStream Skip Line
No way.. What if my first line is like 2 characters long but sometimes 6? I mean.. There has to be something. Also, what about reading the line? Like.. Read the entire line and put it into a string
BTW:
An example would be nice
Last edited by Fromethius; Apr 8th, 2006 at 10:36 PM.
-
Apr 8th, 2006, 10:51 PM
#4
Re: [2.0] BaseStream Skip Line
Your reading about the StreamReader class would also be nice. The most commonly used method of the StreamReader is ReadLine.
VB Code:
Dim lineNumberToRead as Integer = 10 'Read the tenth line.
Dim linesRead As Integer = 0
Dim line As String
Do
'Read the line and discard it.
line = myStreamReader.ReadLine()
lineRead += 1
Loop Until linesRead = lineNumberToRead
MessageBox.Show(line)
Note that I'm treating the line numbers as 1-based, rather than an index that is zero-based. There would be all sorts of variations that would produce the same result.
-
Apr 8th, 2006, 10:55 PM
#5
Re: [2.0] BaseStream Skip Line
Oops. Let's try that in C# instead.
Code:
int lineNumberToRead = 10; // Read the tenth line.
int linesRead = 0;
string line = null;
do
{
line = myStreamReader.ReadLine();
linesRead += 1;
} while (linesRead < lineNumberToRead);
MessageBox.Show(line);
-
Apr 8th, 2006, 10:59 PM
#6
Thread Starter
Frenzied Member
Re: [2.0] BaseStream Skip Line
Thanks I'll try that
Thanks JMC, you're a life saver
Last edited by Fromethius; Apr 8th, 2006 at 11:22 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|