CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2009
    Posts
    6

    [RESOLVED] Readline help?

    Hi folks

    Im writing a small database program, mainle for the practice, and ive run into a problem, that i cant really solve.

    The situation is that i want to start from line 1 of my database (a .txt database), then skip all line until i reach a semicolon ( ; ), then read the line below that semicolon, then skip until the next semicolon, and read the line below that one. I believe you get the picture.

    Ive tried to create another, small database, containing information about where all semicolons were, but this seemed clumsy, and like a very bad way of doing it.

    All help is greatly appeciated

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Readline help?

    if this is the format of your .txt file:

    Code:
    line1
    some text;
    some more text
    event more text;
    more more more
    then this might be what you want:

    Code:
        string[] lines = File.ReadAllLines(@"C:\myfile.txt");
        bool printline = false;
        foreach(string line in lines)
        {
            if(printline)
            {
                Console.WriteLine(line);
                printline = false;
            }
            if(line.EndsWith(";"))
                printline= true;
        }
    this produces this output:
    Code:
    some more text
    more more more
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jul 2009
    Posts
    6

    Re: Readline help?

    Actually, the semicolons are on a seperate line, but that solution you have there actually seems very good. Thanks a lot !

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Readline help?

    Quote Originally Posted by foamy View Post
    if this is the format of your .txt file:

    Code:
    line1
    some text;
    some more text
    event more text;
    more more more
    then this might be what you want:

    Code:
        string[] lines = File.ReadAllLines(@"C:\myfile.txt");
        bool printline = false;
        foreach(string line in lines)
        {
            if(printline)
            {
                Console.WriteLine(line);
                printline = false;
            }
            if(line.EndsWith(";"))
                printline= true;
        }
    this produces this output:
    Code:
    some more text
    more more more
    wouldn't that code not print the first line if it had a semicolon and instead it would print the second line?

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Readline help?

    Form your first post...

    The situation is that i want to start from line 1 of my database (a .txt database), then skip all line until i reach a semicolon ( ; ), then read the line below that semicolon, then skip until the next semicolon, and read the line below that one. I believe you get the picture.
    That is what you wanted, right?

  6. #6
    Join Date
    Jul 2009
    Posts
    6

    Re: Readline help?

    Yes, that is indeed what i wanted, thank you a lot !

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: [RESOLVED] Readline help?

    Never mind, misread something.

  8. #8
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Readline help?

    Quote Originally Posted by eclipsed4utoo View Post
    wouldn't that code not print the first line if it had a semicolon and instead it would print the second line?
    yep, that's what he wanted

    turns out the syntax for his file is this:
    Code:
    line1
    some text
    ;
    some more text
    even more text
    ;
    more more more
    this will result in the same output, but if this was the file:

    Code:
    ;
    line1
    some text
    ;
    some more text
    even more text
    ;
    more more more
    line1 would be printed too
    It's not a bug, it's a feature!

Tags for this Thread

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

Featured