none
Adjust the codes

    Question

  • Hi,
    I want to split one file into several others, and each file is having 5000 lines by these

                    StreamWriter writer = null;
                    string N0 = "F" + F0.ToString().Trim();
                    using (StreamReader sr = new StreamReader(@"c:/dp2/inp.txt", Encoding.Default))
                    {
    
                        while ((line = sr.ReadLine()) != null)
                        {
                            if (writer == null || C0 > 8000)
                            {
                                if (writer != null)
                                {
                                    writer.Close();
                                    writer = null;
                                }
    
                                writer = new System.IO.StreamWriter(@"c:/dp2/" + N0 + ".txt", true);
    
                                C0 = 0;
                            }
                            C0++;
                        }
                    }


    what to adjust to get what I expect to have?


    Many Thanks & Best Regards, Hua Min



    • Edited by HuaMin Chen Thursday, April 27, 2017 2:53 AM
    Wednesday, April 26, 2017 2:52 AM

Answers

  • I'm not sure what you're after. Is this the sort of thing?

       class Program
       {
          static void Main(string[] args)
          {
             string inputFile = "c:\\test\\input.txt"; // The file to split.
             string outputFolder = "c:\\test\\";
             string outputFile = "output"; // The prefix of the output file names (so, "output0", "output1", "output2" and so on.
             string outputExtension = ".txt";
             int maxOutputLines = 5000; // The maximum number of lines in one output file.
    
             string line;
    
             using (StreamReader sr = new StreamReader(inputFile, Encoding.Default))
             {
                int nlines = 0;
                int nfile = 0;
                StreamWriter writer = null;
                while ((line = sr.ReadLine()) != null)
                {
                   if (nlines == 0)
                   {
                      if (writer != null)
                      {
                         writer.Close();
                      }
                      writer = new StreamWriter(outputFolder + outputFile + nfile.ToString() + outputExtension, true);
                      nfile++;
                   }
    
                   writer.WriteLine(line);
    
                   nlines++;
                   if (nlines >= maxOutputLines)
                      nlines = 0;
    
                }
    
                if(writer != null)
                   writer.Close();
    
             }
          }
       }
    

    • Marked as answer by HuaMin Chen Thursday, April 27, 2017 4:36 AM
    Thursday, April 27, 2017 4:20 AM

All replies

  • Any help?

    Many Thanks & Best Regards, Hua Min

    Thursday, April 27, 2017 2:53 AM
  • I'm not sure what you're after. Is this the sort of thing?

       class Program
       {
          static void Main(string[] args)
          {
             string inputFile = "c:\\test\\input.txt"; // The file to split.
             string outputFolder = "c:\\test\\";
             string outputFile = "output"; // The prefix of the output file names (so, "output0", "output1", "output2" and so on.
             string outputExtension = ".txt";
             int maxOutputLines = 5000; // The maximum number of lines in one output file.
    
             string line;
    
             using (StreamReader sr = new StreamReader(inputFile, Encoding.Default))
             {
                int nlines = 0;
                int nfile = 0;
                StreamWriter writer = null;
                while ((line = sr.ReadLine()) != null)
                {
                   if (nlines == 0)
                   {
                      if (writer != null)
                      {
                         writer.Close();
                      }
                      writer = new StreamWriter(outputFolder + outputFile + nfile.ToString() + outputExtension, true);
                      nfile++;
                   }
    
                   writer.WriteLine(line);
    
                   nlines++;
                   if (nlines >= maxOutputLines)
                      nlines = 0;
    
                }
    
                if(writer != null)
                   writer.Close();
    
             }
          }
       }
    

    • Marked as answer by HuaMin Chen Thursday, April 27, 2017 4:36 AM
    Thursday, April 27, 2017 4:20 AM