2011-10-05 39 views
4

我試圖讀取C#中的文本文件並將行號添加到行中。C#將行號添加到文本文件

這是我的輸入文件:

This is line one 
    this is line two 
    this is line three 

,這應該是輸出:

1 This is line one 
    2 this is line two 
    3 this is line three 

這是我到目前爲止的代碼:

class Program 
{ 
    public static void Main() 
    { 
     string path = Directory.GetCurrentDirectory() + @"\MyText.txt"; 

     StreamReader sr1 = File.OpenText(path); 

     string s = ""; 

     while ((s = sr1.ReadLine()) != null)   
     { 
      for (int i = 1; i < 4; i++) 
       Console.WriteLine(i + " " + s); 
      } 

      sr1.Close(); 
      Console.WriteLine();  
      StreamWriter sw1 = File.AppendText(path); 
      for (int i = 1; i < 4; i++) 
      { 
       sw1.WriteLine(s); 
      } 

      sw1.Close();    
    } 
} 

我相信,我90%需要使用循環來獲取行號,但到目前爲止,使用此代碼我可以在控制檯中獲得該輸出:

1 This is line one 
2 This is line one 
3 This is line one 
1 this is line two 
2 this is line two 
3 this is line two 
1 this is line three 
2 this is line three 
3 this is line three 

而且這是在輸出文件:即使它是前面定義的文件(另一塊在書寫時

This is line number one. 
This is line number two. 
This is line number three.1 
2 
3 

我不知道爲什麼不使用字符串變量s,另一個規則可能?)。

+1

一般評論:我認爲最好使用(StreamReader){}和使用(StreamWriter){}塊。而且你也應該命名你的變量的''行',這是更清晰的,因爲它是一個行:) –

+1

我不知道這是問題或你的代碼的問題,但你的括號不匹配。 while循環比您從縮進中想到的更早關閉。 – Ray

+1

爲什麼你要從'1'循環到'4'?你想重複每一行四次嗎? –

回答

2
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Text; 

namespace AppendText 
{ 
    class Program 
    { 
     public static void Main() 
     { 
      string path = Directory.GetCurrentDirectory() + @"\MyText.txt"; 

      StreamReader sr1 = File.OpenText(path); 


      string s = ""; 
      int counter = 1; 
      StringBuilder sb = new StringBuilder(); 

      while ((s = sr1.ReadLine()) != null) 
      { 
       var lineOutput = counter++ + " " + s; 
       Console.WriteLine(lineOutput); 

       sb.Append(lineOutput); 
      } 


      sr1.Close(); 
      Console.WriteLine(); 
      StreamWriter sw1 = File.AppendText(path); 
      sw1.Write(sb); 

      sw1.Close(); 

     } 

    } 
} 
+0

在作業問題中,通常不給出問題的完整解決方案。此外,您的程序將新文本連接到舊的顯然不希望的。 – Chris

+0

我設法改變它,現在它創建一個新的文件使用:StreamWriter sw1 = File.CreateText(Mytext2.txt);所以現在它創建一個新文件。然而。新創建的文件中的輸出在一行上。我知道我必須更改stringbuilder類中的代碼,並且可以將字符插入到行中:sb.Append(「testing」+ lineOutput);並且它們出現在每一行句子的前面,但是當我想爲新行插入\ n時,它不起作用。 – Vojtech

+2

@Chris,抱歉沒有意識到這是作業。刪除我的帖子會做?! :) –

0

您需要在每行字符串前添加行號。檢查出String.Format。此外,請嘗試一個位於while循環之外的計數器變量來保持行數。

希望這足以讓您走上正確的道路,而不必向您提供確切的答案。

0

您確定要關閉循環內的流while

+0

我認爲流在while循環後關閉,因爲它是在}括號後面 – Vojtech

1

OPEN STREAM

讀取整行並將其存儲在臨時變量中。 使用計數器來跟蹤您已閱讀的行。 將計數器與臨時變量連接起來。 將其保存到文件中。 將您的線指針移至下一行並重復 。

然後關閉STREAM

2
IEnumerable<string> lines = File.ReadLines(file) 
           .Select((line,i)=>i + " " + line) 
           .ToList(); 
File.WriteAllLines(file, lines); 
+0

我認爲你沒有得到它是作業,你認爲你真的幫助他嗎? –

+1

@BaptistePernet瞭解我的代碼足夠功課。他會通過試圖找出它的工作原理來學習很多東西。 –

+1

@Hanan絕對。 SO的重點在於通過高質量的答案進行教育,而不僅僅是讓某人完成作業。謝謝。 –

1

我可以爲您提供正確的代碼,但因爲是下班回家我只問你的問題應該引導你正確的答案:

  • 爲什麼在循環內部關閉StreamReader?您仍然可以訪問它,這可能會導致錯誤。
  • 爲什麼你在沒有前置索引的情況下寫入StreamWriter?
  • 爲什麼你打開循環內的StreamWriter?在循環之外打開StreamWriter和StreamReader不是更好嗎?你是否在循環中工作,然後關閉Streams?
0

當心的FOR循環,你把他們在裏面,所以基本上你說:

while ((s = sr1.ReadLine()) != null) 

每一行讀

for (int i = 1; i < 4; i++) 

重複3次的寫操作。

另外,您正在關閉流中的內容,因此在讀取第一行之後。

0

這是給你一個大問題:

 for (int i = 1; i < 4; i++) 
      Console.WriteLine(i + " " + s); 
     } 

你用大括號關閉的循環,但不使用大括號將其打開。這意味着上面引用的大括號實際上是關閉了while循環,所以你循環完成所有的console.writeline,然後當你寫入文件時,你實際上並沒有從文件中讀取 - s是「」due進行範圍界定。

+0

閉合大括號屬於while循環(縮進是錯誤的),for循環沒有兩個大括號。 –

+1

@Giuseppe R:我認爲它更有可能實際上括號是錯誤的,而不是故意在那裏結束while循環,但我很樂意糾正。無論哪種方式,肯定有一個錯誤,因爲while循環無法做任何有用的事情,因爲顯然你要寫入文件的動作必須爲每行讀取完成。 – Chris