2009-12-18 105 views
0

特定字符的文件,我有一个名为C:/test.txt的文本文件:阅读在包含在C#

 
1 2 3 4 
5 6 

我想读的使用StreamReader在这个文件中的每个号码。

我该怎么做?

+0

会将文件包含*只有*号,也可能有其他字符? – 2009-12-18 12:22:58

+0

您的问题似乎与您的示例无关。数字不是特殊字符... – James 2009-12-18 12:37:19

回答

5

你真的需要使用StreamReader做到这一点?

IEnumerable<int> numbers = 
    Regex.Split(File.ReadAllText(@"c:\test.txt"), @"\D+").Select(int.Parse); 

(显然,如果这是不切实际的读取整个文件的一重击,那么你就需要在流,但如果你能使用File.ReadAllText那么这是应该做的方式,在我看来。 )

为了完整起见,这里是一个流版本:

public IEnumerable<int> GetNumbers(string fileName) 
{ 
    using (StreamReader sr = File.OpenText(fileName)) 
    { 
     string line; 
     while ((line = sr.ReadLine()) != null) 
     { 
      foreach (string item in Regex.Split(line, @"\D+")) 
      { 
       yield return int.Parse(item); 
      } 
     } 
    } 
} 
0

我可能错了,但使用StreamReader你不能设置分隔符。 但是你可以使用String.Split()来设置分隔符(它是你的情况下的空间?)并将所有数字提取到单独的数组中。

0

事情是这样的:

using System; 
using System.IO; 

class Test 
{ 

    public static void Main() 
{ 
    string path = @"C:\Test.txt"; 

    try 
    { 
     if(File.Exists(path)) 
     { 
     using(StreamReader sr = new StreamReader(path)) 
     { 
      while(sr.Peek() >= 0) 
      { 
      char c = (char)sr.Read(); 
      if(Char.IsNumber(c)) 
       Console.Write(c); 
      } 
     } 
     } 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("The process failed: {0}", e.ToString()); 
    } 
} 
} 
+0

呃...也许我错过了这里的东西(这很冷,我的大脑不喜欢寒冷)。为什么要删除输入文件,然后尝试打开它? – ZombieSheep 2009-12-18 12:37:30

+0

ahah你是对的xD我正在写速度和intellisense使笑话:) – Salv0 2009-12-18 12:42:09

0

像这样的东西应该工作:

using (var sr = new StreamReader("C:/test.txt")) 
{ 
    var s = sr.ReadToEnd(); 
    var numbers = (from x in s.Split('\n') 
        from y in x.Split(' ') 
        select int.Parse(y)); 
} 
1
using (StreamReader reader = new StreamReader(stream)) 
{ 
    string contents = reader.ReadToEnd(); 

    Regex r = new Regex("[0-9]"); 

    Match m = r.Match(contents); 

    while (m.Success) 
    { 
    int number = Convert.ToInt32(match.Value); 

    // do something with the number 

    m = m.NextMatch(); 
    } 

} 
+0

好的答案,但slooow – bniwredyc 2009-12-18 13:01:16

+0

我的意思是解决方案很慢没有答案(% – bniwredyc 2009-12-18 13:11:27

1

像这样可能做的伎俩,如果你想要的东西是要读取一个整数文件并将它们存储在列表中。

try 
{ 
    StreamReader sr = new StreamReader("C:/test.txt")) 
    List<int> theIntegers = new List<int>(); 
    while (sr.Peek() >= 0) 
    theIntegers.Add(sr.Read()); 
    sr.Close(); 
} 
catch (Exception e) 
{ 
    //Do something clever to deal with the exception here 
} 
1

解决方案为大文件:

class Program 
{ 
    const int ReadBufferSize = 4096; 

    static void Main(string[] args) 
    { 
     var result = new List<int>(); 

     using (var reader = new StreamReader(@"c:\test.txt")) 
     { 
      var readBuffer = new char[ReadBufferSize]; 
      var buffer = new StringBuilder(); 

      while ((reader.Read(readBuffer, 0, readBuffer.Length)) > 0) 
      { 
       foreach (char c in readBuffer) 
       { 
        if (!char.IsDigit(c)) 
        { 
         // we found non digit character 
         int newInt; 
         if (int.TryParse(buffer.ToString(), out newInt)) 
         { 
          result.Add(newInt); 
         } 

         buffer.Remove(0, buffer.Length); 
        } 
        else 
        { 
         buffer.Append(c); 
        } 
       } 
      } 

      // check buffer 
      if (buffer.Length > 0) 
      { 
       int newInt; 
       if (int.TryParse(buffer.ToString(), out newInt)) 
       { 
        result.Add(newInt); 
       } 
      } 
     } 

     result.ForEach(Console.WriteLine); 
     Console.ReadKey(); 
    } 
}