Reputation: 33
I use the follow code to read a text file lines and create a new one (it's working):
StreamReader Read2;
Read2 = File.OpenText("save" + campionatoselezTxt.Text + "bex.txt");
StreamWriter Write2 = File.CreateText("read" + campionatoselezTxt.Text + "bex.txt");
while (!Read2.EndOfStream)
{
string NewLine = "";
for (int K = 0; K < 8; K++)
{
if (K != 0)
NewLine = NewLine + ";" + Read2.ReadLine();
else
NewLine = Read2.ReadLine();
}
Write2.WriteLine(NewLine);
}
Read2.Close();
Write2.Close();
With this code, I read till the end of text file and put gropups of lines (8 for group) with a specific separator (;), but I'd like to stop to read source text file when I find a specific word: "Betting. How can I do it? Thanks! :)
EDIT: This is an example of source text file:
Slask
Termalica
2
0
1.67
3.54
5.11
02.08.2015
Podbeskidzie
Cracovia
0
1
2.88
3.16
2.41
01.08.2015
Wisla
Lech
2
0
3.13
3.18
2.26
01.08.2015
Lechia
Pogon
1
1
1.76
3.45
4.54
31.07.2015
Piast
GornikZ
3
2
2.45
3.18
2.82
31.07.2015
GornikL
GornikZ
2
1
2.45
3.07
2.89
27.07.2015
Jagiellonia
Termalica
2
0
2.04
3.27
3.53
26.07.2015
Legia
Podbeskidzie
5
0
1.29
4.97
9.69
26.07.2015
Pogon
Slask
1
1
2.31
3.19
3.02
26.07.2015
Lech
Lechia
2
1
1.93
3.32
3.82
25.07.2015
Zaglebie
Korona
0
2
1.81
3.33
4.43
25.07.2015
Cracovia
Wisla
1
1
2.19
3.23
3.22
24.07.2015
Ruch
Piast
2
0
2.50
3.09
2.83
24.07.2015
Piast
Termalica
1
0
2.47
3.13
2.83
20.07.2015
Korona
Jagiellonia
3
2
2.31
3.25
2.96
19.07.2015
Slask
Legia
1
4
3.16
3.18
2.23
19.07.2015
Lech
Pogon
1
2
1.49
3.87
6.63
18.07.2015
Ruch
GornikL
0
2
2.25
3.12
3.19
18.07.2015
Zaglebie
Podbeskidzie
1
1
1.87
3.32
4.16
18.07.2015
Lechia
Cracovia
0
1
1.91
3.28
3.98
17.07.2015
Wisla
GornikZ
1
1
1.92
3.35
3.86
17.07.2015
Betting
odds
service provided
in
cooperation
with
OddsPortal.com
And this is an example of destination file:
Slask;Termalica;2;0;1.67;3.54;5.11;02.08.2015
Podbeskidzie;Cracovia;0;1;2.88;3.16;2.41;01.08.2015
Wisla;Lech;2;0;3.13;3.18;2.26;01.08.2015
Lechia;Pogon;1;1;1.76;3.45;4.54;31.07.2015
Piast;GornikZ;3;2;2.45;3.18;2.82;31.07.2015
GornikL;GornikZ;2;1;2.45;3.07;2.89;27.07.2015
Jagiellonia;Termalica;2;0;2.04;3.27;3.53;26.07.2015
Legia;Podbeskidzie;5;0;1.29;4.97;9.69;26.07.2015
Pogon;Slask;1;1;2.31;3.19;3.02;26.07.2015
Lech;Lechia;2;1;1.93;3.32;3.82;25.07.2015
Zaglebie;Korona;0;2;1.81;3.33;4.43;25.07.2015
Cracovia;Wisla;1;1;2.19;3.23;3.22;24.07.2015
Ruch;Piast;2;0;2.50;3.09;2.83;24.07.2015
Piast;Termalica;1;0;2.47;3.13;2.83;20.07.2015
Korona;Jagiellonia;3;2;2.31;3.25;2.96;19.07.2015
Slask;Legia;1;4;3.16;3.18;2.23;19.07.2015
Lech;Pogon;1;2;1.49;3.87;6.63;18.07.2015
Ruch;GornikL;0;2;2.25;3.12;3.19;18.07.2015
Zaglebie;Podbeskidzie;1;1;1.87;3.32;4.16;18.07.2015
Lechia;Cracovia;0;1;1.91;3.28;3.98;17.07.2015
Wisla;GornikZ;1;1;1.92;3.35;3.86;17.07.2015
So, I want lines before the line where there is the word "Betting"
Upvotes: 0
Views: 4019
Reputation: 1235
I guess you can have problems with this line if you find the streams ends before the eight lines.
NewLine = NewLine + ";" + Read2.ReadLine();
I've made some changes to your code, test and adapt it if you want:
using (StreamReader read2 = File.OpenText("save" + campionatoselezTxt.Text + "bex.txt"))
{
using (StreamWriter write2 = File.CreateText("read" + campionatoselezTxt.Text + "bex.txt"))
{
int k = 0;
string newLine = string.Empty;
while (!read2.EndOfStream)
{
if (k == 8)
{
write2.WriteLine(newLine);
k = 0;
newLine = string.Empty;
}
newLine = string.IsNullOrEmpty(newLine) ? read2.ReadLine() : newLine + ";" + read2.ReadLine();
k++;
if (newLine.Contains("Betting"))
{
write2.WriteLine(newLine.Substring(0, newLine.IndexOf("Betting")));
break;
}
}
}
}
Upvotes: 0
Reputation: 1753
your question isnt too clear so its hard to know where exactly you want to stop but something like this will probably work....
StreamReader Read2;
Read2 = File.OpenText("save" + campionatoselezTxt.Text + "bex.txt");
StreamWriter Write2 = File.CreateText("read" + campionatoselezTxt.Text + "bex.txt");
while (!Read2.EndOfStream)
{
string NewLine = "";
for (int K = 0; K < 8; K++)
{
if (K != 0)
NewLine = NewLine + ";" + Read2.ReadLine();
else
NewLine = Read2.ReadLine();
}
if(NewLine.Contains("Specific Word"))
break;
Write2.WriteLine(NewLine);
}
Read2.Close();
Write2.Close();
Upvotes: 1