I want to create a Windows Forms app in Visual Studio that writes text files on a button click.
I have a txt file (e.g. test.txt) which contains
AAAA
BBBB
CCCC
DDDD
EOS
FFFF
GGGG
HHHH
IIII
EOS
JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF
Then I would like to split it into number of other txt files
**bag1.txt**
AAAA
BBBB
CCCC
DDDD
EOS
**bag2.txt**
EEEE
FFFF
GGGG
IIII
EOS
**bag3.txt**
JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF
these is the code,
private void read3btn_Click(object sender, EventArgs e)
{
string fileName = textBox1.Text;
TextReader sr = new StreamReader(fileName);
//This allows you to do one Read operation.
string s = sr.ReadToEnd();;
sr.Close();
string[] bags = s.Split(new string[] {"EOS"}, StringSplitOptions.None);
// This will give you an array of strings (minus the EOS field)
// Then write the files...
System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag1.txt", bags[0] + "EOS"); //< -- Add this you need the EOS at the end field the field
System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag2.txt", bags[1] + "EOS");
System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag3.txt", bags[2] + "EOS" + bags[3]);
}}
Then the output comes this way
**bag1.txt**
AAAA
BBBB
CCCC
DDDD
EOS
**bag2.txt**
EEEE
FFFF
GGGG
IIII
EOS
**bag3.txt**
JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF
unfortunately the output in bags[1] and bags[2] has a blank line in the first line, is there anyway to update the code?