Avatar of Peter Chan
Peter Chan
Flag for Hong Kong asked on

Adjust the codes

Hi,
I want to split one file into several others, and each file is having 5000 lines by these
                string N0 = "F" + F0.ToString().Trim();
                using (StreamReader sr = new StreamReader(@"c:/dp2/inp.txt", Encoding.Default))
                {

                    while ((line = sr.ReadLine()) != null)
                    {
                        {
                            using (StreamWriter sw = new StreamWriter(@"c:/dp2/"+N0+".txt"))
                            {
                                sw.WriteLine(line);
                            }
                            
                            if (C0 % 5000 == 0)
                            {
                                F0++;
                                N0 = "F" + F0.ToString().Trim();
                            }
                            C0++;
                        }
                    }
                }

Open in new window

what to adjust to get what I expect to have?
.NET ProgrammingC#Programming Languages-Other

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
Milos Ilic

This one simple solution in C#, but on side of memory consumption is not the optimal solution.


using (System.IO.StreamReader sr = new System.IO.StreamReader("path"))
{
    int fileNumber = 0;

    while (!sr.EndOfStream)
    {
        int count = 0;

        using (System.IO.StreamWriter sw = new System.IO.StreamWriter("other path" + ++fileNumber))
        {
            sw.AutoFlush = true;

            while (!sr.EndOfStream && ++count < 5000)
            {
                sw.WriteLine(sr.ReadLine());
            }
        }
    }
}

Open in new window




This second solution is not COMPLITE, but will be better. I wrote it on the fly also in C#. You will need to add one more loop and counter for files.

int count = 0;
string line;
TextReader reader = new StreamReader("file.txt");
using (var result = CreateText("result.txt"))
{
	while ((line = reader.ReadLine()) != null)
	{
				count++;
				if ( count >= 5000 ) 
				{
					result.WriteLine(line);
				} 
	  
	}
	reader.Close();
}

Open in new window

Peter Chan

ASKER
Sorry to that I do not know why it is creating the file only having one line inside. What to adjust below?
                string N0 = "F" + F0.ToString().Trim();
                using (StreamReader sr = new StreamReader(@"c:/dp2/inp.txt", Encoding.Default))
                {

                    while ((line = sr.ReadLine()) != null)
                    {
                        {
                            using (StreamWriter sw = new StreamWriter(@"c:/dp2/" + N0 + ".txt"))
                            {
                                sw.AutoFlush = true;
                                sw.WriteLine(line);
                            }

                            if (C0 % 8000 == 0 && C0 >= 8000)
                            {
                                F0++;
                                N0 = "F" + F0.ToString().Trim();
                            }
                            C0++;
                        }
                    }
                }

Open in new window

ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck