C#streamwriter创建部分文件

时间:2013-03-12 12:44:38

标签: c# text-files streamreader streamwriter multiple-files

我正在尝试创建一个哈希文本文件。代码可以工作,问题是一旦流程图启动过程,它就不会停止直到它完成。我想将输出文件分解成更小的部分。如何在不重新开始处理的情况下停止编写器并启动新文件?

string infile = @"ntlmchar.txt";
        string hashfile = @"ntlmhash.txt"; //File that includes the hash and clear test
        string charfile = @"ntlmchar.txt"; //File that only has the clear text
        string oldCharFile = ""; //Temp file to apply to infile.
        int cint = 1;  //The number of characters in the file
        string str_cint = cint.ToString(); //convert cint to string
        int pint = 1; //The number of parts to the character file
        string str_pint = pint.ToString(); //convert pint to string
        int cm = 4; //Max number of characters
        int pm = 4000; //Man number of parts
        int line = 0; //line index number

        while (cint <= cm)
        {
            if (!File.Exists(infile))
            {
                for (int ci =1; ci <= cm; ci++)
                {
                    str_cint = cint.ToString();
                    for (int pi =1; pi <= pm; pi++)
                    {
                        str_pint = pint.ToString();
                       // System.Console.WriteLine("Inner for loop cint file does not exist" +cint +" pint " + pint);
                     //   System.Console.WriteLine("Inner for loop str_cint file does not exist " + str_cint + " cint " + cint);
                        charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt";
                        pint = pi;
                        oldCharFile = charfile;
                        infile = oldCharFile;
                        if (File.Exists(infile)) break;
                    //    System.Console.WriteLine("inner loop file " + infile);
                    }
              //      System.Console.WriteLine("outer for loop cint " + cint + " pint " + pint);             
                  //  System.Console.WriteLine("infile not found " + infile + " " + oldCharFile + " " + charfile + " " + hashfile);
                }
              //  System.Console.WriteLine("No work files found " + infile + " " + oldCharFile + " " + charfile + " " + hashfile);
            }

            else if (File.Exists(infile))
            {
                // Create a file to write to. 
            //    System.Console.WriteLine("cint at the start of else if " + cint + " str_cint " + str_cint);
                infile = oldCharFile;
                str_cint = cint.ToString();
         //       System.Console.WriteLine("cint after assign to str_cint " + cint + " str_cint " + str_cint);
                pint=1;
                str_pint = pint.ToString();
                hashfile = "ntlmhash" + str_cint + "_" + str_pint + ".txt";
                charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt";
                //System.Console.WriteLine(infile + " " + oldCharFile + " " + charfile + " " + hashfile);
       //         System.Console.WriteLine("Infile found " + cint + " " + pint);
                using (StreamWriter h = new StreamWriter(hashfile))
                using (StreamWriter c = new StreamWriter(charfile))
                using (StreamReader sr = new StreamReader(infile))
                {
                    string i = "";
                    while ((i = sr.ReadLine()) != null)
                    {
                        foreach (string s in alpha)
                        {

                            if (line <= 2000000)
                            {
                                string j = i + s;
                                string str = Program.Ntlm(j);
                                hashfile = "ntlmhash" + str_cint + "_" + str_pint + ".txt";
                                charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt";
                       //         System.Console.WriteLine("line before writing to file " + line + " in charfile " + charfile);
                                h.WriteLine("{0}, {1}", j, str);
                                c.WriteLine("{0}", j);
                                line++;

                         //       System.Console.WriteLine("h file" + h + " c file" + c);

                            }
                            else
                            {
                                h.Flush();
                                c.Flush();
                                pint++;
                                str_pint = pint.ToString();
                                hashfile = "ntlmhash" + str_cint + "_" + str_pint + ".txt";
                                charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt";
                                line = 1;
                                System.Console.WriteLine("line after writing to part of file " + line + " in charfile " + charfile);
                            }

                        }
                    }

1 个答案:

答案 0 :(得分:1)

我假设您试图为每个文件获得2,000,000个项目?你只需要进行一些重组。

现在你有:

using (StreamWriter h = new StreamWriter(hashfile))
using (StreamWriter c = new StreamWriter(charfile))
using (StreamReader sr = new StreamReader(infile))
{
    string i = "";
    while ((i = sr.ReadLine()) != null)
    {

您需要更改代码,以便稍后打开输出文件:

using (StreamReader sr = new StreamReader(infile))
{
    StreamWriter h = null;
    StreamWriter c = null;
    try
    {
        h = new StreamWriter(...);
        c = new StreamWriter(...);
        string i = "";
        while ((i = sr.ReadLine()) != null)
        {
            // output line here
            // and increment line counter.
            ++line;
            if (line > 2000000)
            {
                // Close the output files and open new ones
                h.Close();
                c.Close();
                h = new StreamWriter(...);
                c = new StreamWriter(...);
                line = 1;
            }
        }
    }
    finally
    {
        if (h != null) h.Close();
        if (c != null) c.Close();
    }
}