Results 1 to 6 of 6

Thread: [RESOLVED] [2.0] BaseStream Skip Line

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim lineNumberToRead as Integer = 10 'Read the tenth line.
    2. Dim linesRead As Integer = 0
    3. Dim line As String
    4.  
    5. Do
    6.     'Read the line and discard it.
    7.     line = myStreamReader.ReadLine()
    8.  
    9.     lineRead += 1
    10. Loop Until linesRead = lineNumberToRead
    11.  
    12. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    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
  •  



Click Here to Expand Forum to Full Width