2011-10-05 42 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