2014-01-30 50 views
0

我有一個名爲info.txt的文本文件,其中包含字符串。計數子串發生

info.txt

05331 
02555 
03211 
05222 
04321 
02387 
03444 
03127 
05117 
03680 
03881 
01579 
03111 

我的輸出應該在new.txt

輸出new.txt

05331 
02555 
03211 
1 
05222 
04321 
02387 
03444 
03127 
2 
05117 
03680 
03881 
01579 
03111 
3 

基本上我應該得到的所有字符串的計數開始「03 「並在子串」01「之前打印計數

 try 
     { 
      String line; 
      Int counter =0; 
      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) == "05") 
       { 
        sw.Write(counter.ToString()); 
        counter =0; 
       } 
       If (line.subString(0,2) =="03") 
       { 
        //loop 
        counter++; 
       } 

       sw.WriteLine(line); 
      } 

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

寫完我的代碼後,我只能得到。

0 
05331 
02555 
03211 
1 
05222 
04321 
02387 
03444 
03127 
2 
05117 
03680 
03881 
01579 
03111 

第一行的0不應該是因爲我之前沒有蜇傷,並且上次計數也沒有計數。

請幫幫忙。

+3

讀過你的樣品兩次,仍然不能得到你想要得到的數字邏輯。你說你想要在子字符串「01」之前打印計數,但是在'05'之前打印它的期望輸出。 –

+0

由於文件的第一行從05開始立即寫出計數器。基本上,你需要讀取AHEAD,而不是BEHIND,也就是說,在決定寫出計數器之前,你需要知道下一行是什麼。 –

+0

根據您提供的輸出,看起來像邏輯是每次以「05」開頭的字符串在文件末尾出現AND並打印計數。它是否正確? – etaiso

回答

2

而不是你的while loop你可以嘗試類似的東西:對於使用預讀執行

... 
Boolean isFirstLine = true; 

while ((line = sr.ReadLine()) != null) { 
    // If line starts with "05" we should print out counter 
    // (that is number of "03" started lines) 
    // unless it is the first line in the file 
    if (line.StartsWith("05")) { 
    if (!isFirstLine) 
     sw.WriteLine(counter.ToString()); 

    sw.WriteLine(line); 
    counter = 0; 
    isFirstLine = false; 

    continue; 
    } 

    sw.WriteLine(line); 

    if (line.StartsWith("03")) 
    counter += 1; 

    // We should also print out counter after the last file line 
    // if, say, counter > 0 
    if (sr.Peek() < 0) // <- No next line 
    if (counter > 0) 
     sw.WriteLine(counter.ToString()); 

    isFirstLine = false; 
} 
... 
1

這裏的代碼。就在循環結束之前,你讀下一行,如果是空(文件末尾),或者以「05」開頭,那麼你的輸出和復位計數器你[R幫助傢伙

  try 
      { 
       int counter = 0; 
       //Pass the file path and name to the StreamReader constructer 
       StreamReader sr = new StreamReader("gamenam.txt"); 
       //Pass the file path and name to the StreamReader constructer 
       StreamWriter sw = new StreamWriter("gamenam_1.txt"); 


       string line = sr.ReadLine(); 
       while (line != null) 
       { 
        if (line.Substring(0, 2) == "03") 
        { 
         counter++; 
        } 

        sw.WriteLine(line); 

        line = sr.ReadLine(); 
        if ((line == null) || (line.StartsWith("05"))) 
        { 
         sw.WriteLine(counter.ToString()); 
         counter = 0; 
        } 
       } 

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

謝謝,從你所輸入的內容中,我終於得到了我需要的輸出。

我寫了我這樣的代碼

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