-
July 7th, 2009, 04:03 AM
#1
[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
-
July 7th, 2009, 04:36 AM
#2
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!
-
July 7th, 2009, 05:13 AM
#3
Re: Readline help?
Actually, the semicolons are on a seperate line, but that solution you have there actually seems very good. Thanks a lot !
-
July 7th, 2009, 09:11 PM
#4
Re: Readline help?
Originally Posted by foamy
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?
-
July 7th, 2009, 09:21 PM
#5
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?
-
July 7th, 2009, 11:49 PM
#6
Re: Readline help?
Yes, that is indeed what i wanted, thank you a lot !
-
July 7th, 2009, 11:53 PM
#7
Re: [RESOLVED] Readline help?
Never mind, misread something.
-
July 8th, 2009, 01:50 AM
#8
Re: Readline help?
Originally Posted by eclipsed4utoo
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|