閱讀在包含在C#
回答
你真的需要使用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);
}
}
}
}
我可能錯了,但使用StreamReader你不能設置分隔符。 但是你可以使用String.Split()來設置分隔符(它是你的情況下的空間?)並將所有數字提取到單獨的數組中。
事情是這樣的:
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());
}
}
}
呃...也許我錯過了這裏的東西(這很冷,我的大腦不喜歡寒冷)。爲什麼要刪除輸入文件,然後嘗試打開它? – ZombieSheep 2009-12-18 12:37:30
ahah你是對的xD我正在寫速度和intellisense使笑話:) – Salv0 2009-12-18 12:42:09
像這樣的東西應該工作:
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));
}
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();
}
}
像這樣可能做的伎倆,如果你想要的東西是要讀取一個整數文件並將它們存儲在列表中。
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
}
解決方案爲大文件:
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();
}
}
- 1. 閱讀包含的XMLReader
- 2. 閱讀在C#
- 3. 閱讀在C++
- 4. 閱讀在C++
- 5. 閱讀數在C
- 6. 閱讀.DOCX在C++
- 7. C++:包含在包含內
- 8. 閱讀本書在C/C++
- 9. 閱讀在包中數學
- 10. 閱讀包含多個文件的MemoryStream
- 11. DirectShow的閱讀包含回調方法
- 12. Pagebreak包含閱讀更多鏈接
- 13. 使用XSL閱讀包含文件
- 14. 閱讀包含數組的JSON數組(?)C#
- 15. 在二進制文件中包含結構的一個未知數閱讀(C#)
- 16. 閱讀文字在C#
- 17. 如何閱讀在C#
- 18. 閱讀整數,在C++
- 19. 閱讀在旅行商C++
- 20. 在C++上閱讀GPS
- 21. 閱讀char和在C號
- 22. 閱讀文件在C
- 23. 在C#中閱讀PDF
- 24. 閱讀墊文件在C
- 25. 閱讀文件在C++中
- 26. 閱讀與PREAD(),並在C
- 27. 閱讀在使用C
- 28. 閱讀JavaScriptSerializer C#背在JavaScript
- 29. 在Python中閱讀包含缺失項目的列
- 30. 如何閱讀在ASP.NET MVC中包含#的查詢字符串?
會將文件包含*只有*號,也可能有其他字符? – 2009-12-18 12:22:58
您的問題似乎與您的示例無關。數字不是特殊字符... – James 2009-12-18 12:37:19