2014-01-30 34 views
0

临时数组我问这个之前,但大多数人并不明白我的问题。写作从文本文件

我有两个文本文件。 Gamenam.txt这是我正在阅读的文本文件,以及gam enam_2.txt

gamenam.txt我有一个字符串是这样的:

01456 
02456 
05215 
05111 
01421 
03117 
05771 
01542 
04331 
05231 

我已经写了代码to count number of times substring "05" appears in text file before substring "01"

我被写入到gamenam_1.txt输出是:

01456 
02456 
05215 
05111 
2 
01421 
03117 
05771 
1 
01542 
04331 
05231 
1 

这是我写的,实现

string line; 
int counter = 0; 
Boolean isFirstLine = true; 
try 
{ 
    StreamReader sr = new StreamReader("C:\\Files\\gamenam.txt"); 
    StreamWriter sw = new StreamWriter("C:\\Files\\gamenam_1.txt"); 

    while ((line = sr.ReadLine()) != null) 
    { 
     if (line.Substring(0, 2) == "01") 
     { 
      if (!isFirstLine) 
      { 
       sw.WriteLine(counter.ToString()); 
       counter = 0; 
      } 
     } 

     if (line.Substring(0, 2) == "05") 
     { 
      counter++; 
     } 

     sw.WriteLine(line); 

     if (sr.Peek() < 0) 
     { 
      sw.Write(counter.ToString()); 
     } 

     isFirstLine = false; 
    } 

    sr.Close(); 
    sw.Close(); 
} 
catch (Exception e) 
{ 
    Console.WriteLine("Exception: " + e.Message); 
} 
finally 
{ 
    Console.WriteLine("Exception finally block."); 
} 

该代码是可以正常使用的代码。

现在我必须编写代码在写入行之前打印子串“05”的计数。

我的输出应该是这个样子:

2 
01456 
02456 
05215 
05111 
1 
01421 
03117 
05771 
1 
01542 
04331 
05231 

显然我应该先写行临时字符串数组,计数,然后编写计数,然后从后我临时数组写入线。

我新的发展,使我坚持试图找出如何我做到这一点。

任何帮助将高度赞赏。

+0

打印? –

+0

附注 - 如果你有回答你刚才的问题,然后确保你接受的答案@SergeyBerezovskiy我非常感激这是最有用的,你 –

+0

。对于每个人的投入,因为我已经指出 – Brian02

回答

2

尝试在控制台窗口此

string line; 
int counter = 0; 
Boolean isFirstLine = true; 
try 
{ 
    StreamReader sr = new StreamReader("C:\\Files\\gamenam.txt"); 
    StreamWriter sw = new StreamWriter("C:\\Files\\gamenam_1.txt"); 

    var lines = new List<string>(); //Here goes the temp lines 
    while ((line = sr.ReadLine()) != null) 
    { 
     if (line.Substring(0, 2) == "01") 
     { 
      if (!isFirstLine) 
      { 
       sw.WriteLine(counter.ToString()); //write the number before the lines 
       foreach(var l in lines) 
        sw.WriteLine(l); //actually write the lines 
       counter = 0; 
       lines.Clear(); //clear the list for next round 
      } 
     } 

     if (line.Substring(0, 2) == "05") 
     { 
      counter++; 
     } 

     lines.add(line); //instead of writing, just adds the line to the temp list 

     if (sr.Peek() < 0) 
     { 
      sw.WriteLine(counter.ToString()); //writes everything left 
      foreach(var l in lines) 
       sw.WriteLine(l); 
     } 

     isFirstLine = false; 
    } 

    sr.Close(); 
    sw.Close(); 
} 
catch (Exception e) 
{ 
    Console.WriteLine("Exception: " + e.Message); 
} 
finally 
{ 
    Console.WriteLine("Exception finally block."); 
} 
+0

我是一种新的与文本文件时,我得到你在哪里指定了线错误foreach(var 1行) – Brian02

+0

我试着运行你的代码,但我的输出结果显示所有的行一行一行,并显示,我认为是子字符串“05”的所有行的总数。我的代码想要做的是逐行读取并将行存储在某处,可能是一个列表,甚至是一个数组,暂时打印一个子字符串“05”发生在循环变为子串“01”之前的次数,然后写入然后记下临时对象中的所有行。 – Brian02

+0

它是var“l”的字母,而不是“1”的数字 – ivowiblo