[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
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.
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 :D
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.
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);
Re: [2.0] BaseStream Skip Line
Thanks I'll try that
Thanks JMC, you're a life saver :D