0

我正在尝试创建一个哈希文本文件。代码有效,问题是一旦流编写器启动该过程,它在完成之前不会停止。我想将输出文件分成更小的部分。如何停止 streamwriter 并启动一个新文件而不重新启动该过程?

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);
                            }

                        }
                    }
4

1 回答 1

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();
    }
}
于 2013-03-12T13:32:00.993 回答