We help IT Professionals succeed at work.

Adjust the codes

244 Views
Last Modified: 2017-04-27
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?
Comment
Watch Question

Milos IlicSoftware Engineer

Commented:
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 ChanProblem resolver

Author

Commented:
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

Software Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.