Gamma
Gamma

Reputation: 331

How to split a text file into number of other text files?

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

I have written following code but it only reads source file until the first EOS:

private void filterbtn_Click(object sender, EventArgs e)
{
    List<string> strFind = new List<string>();
    using (StreamReader sr = new StreamReader(textBox1.Text))
    {
        string strIndex;
        while((strIndex = sr.ReadLine()) != null)
        {
            strFind.Add(strIndex);
            if (strIndex.Contains("EOS"))
            {
                break;
            }
        }
    }

    using (StreamWriter sw = new StreamWriter(@"D:\Program-program\tesfile\bag1.txt"))
    {
        foreach(string s in strFind)
        {
            sw.WriteLine(s);
        }

        sw.Close();
    }
}

Can anyone tell what's wrong with the code?

Upvotes: 0

Views: 523

Answers (3)

Michael McGriff
Michael McGriff

Reputation: 803

The below gets your desired results. Probably not the most optimized code but it should get you in the right direction.

static void Test()
{                       
    var allLines = File.ReadAllLines("test.txt");

    int controller = 0;
    var buffer = new List<string[]>();

    foreach (string line in allLines)
    {
        string path = (controller == 0) 
            ? "bag1.txt" : (controller == 1) 
                             ? "bag2.txt" : "bag3.txt";

        buffer.Add(new string[] { path, line });
        if (line == "EOS") { controller++; }
    }

    var fileNames = (from b in buffer select b[0]).Distinct();

    foreach (string file in fileNames)
    {
        File.WriteAllLines(file, (from b in buffer where b[0] == file select b[1]).ToArray());
    }
}

Hope it helps!

Upvotes: 0

Doobiferkin
Doobiferkin

Reputation: 31

If you always use EOS and the end of each string field try something like this:

string s = The input text from test.txt

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(bag1 path, bags[0] + "EOS");  < -- Add this you need the EOS at the end field the field

System.IO.File.WriteAllText(bag2 path, bags[1]);

System.IO.File.WriteAllText(bag3 path, bags[3]);

or somthing more efficient like...

foreach(string bag in bags)
{
  ... write the bag file here
}

Upvotes: 1

Stefan Paul Noack
Stefan Paul Noack

Reputation: 3734

I think you got a typo there:

string FindEOF = strFind.Find(p => p == "EOS");

should be

string FindEOF = strFind.Find(p => p == "EOF");

Upvotes: 0

Related Questions