Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag 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?
Avatar of Milos Ilic
Milos Ilic
Flag of Serbia image

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

Avatar of 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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial