Click here to Skip to main content
15,615,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
I am new to C# .net. I need a program to split a large .txt file into 2 based on delimeter (|) count. If a line of data is having <=60 pipe delimeter it should goes to file 1 , if the line of data is having >60 then it goes to file 2. How i can achieve. Please help me if possible.

What I have tried:

I tried in batch script, but it is not that much efficient. So i prefer C# . I haven't try yet. I will post soon.
Updated 14-Jul-17 7:06am

See here: Counting Lines in a String[^] - it's about lines, but the same techniques (and speed results) will work for pipe characters within a line.
Read the file using File.ReadAllLines, then process each line to get the pipe count.
Share this answer
Prabaharan T 14-Jul-17 12:55pm    
Thanks for your suggestion. I will try this solution.
OriginalGriff 14-Jul-17 13:59pm    
You're welcome!

I’d use Streams for this if the input file is large. You will need a StreamReader and two StreamWriters. Put them inside a using statement so they are closed properly when they go out of scope. Something like this.

const string path = @"C:\temp\Test.txt";
const string pathA = @"C:\temp\testA.txt";
const string pathB = @"C:\temp\testB.txt";
    using (var streamOutA = new StreamWriter(pathA))
    using (var streamOutB = new StreamWriter(pathB))
    using (var streamReader = new StreamReader(path))
        while (streamReader.Peek() >= 0)
            string line = streamReader.ReadLine();
            if ((line.Count(c => c == '|') > 60))
catch (Exception e)
    Console.WriteLine("File Exception thrown {0} ", e.Message);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900