阅读在包含在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/C++
- 8. 阅读在包中数学
- 9. 使用XSL阅读包含文件
- 10. 阅读包含多个文件的MemoryStream
- 11. 阅读包含数组的JSON数组(?)C#
- 12. 在二进制文件中包含结构的一个未知数阅读(C#)
- 13. 阅读ISO-8859类型文件包含特殊字符,如é在C + +
- 14. 阅读char和在C号
- 15. 在C++上阅读GPS
- 16. 阅读JavaScriptSerializer C#背在JavaScript
- 17. 阅读垫文件在C
- 18. 阅读文件在C
- 19. 阅读文件在C++中
- 20. 阅读文字在C#
- 21. 阅读与PREAD(),并在C
- 22. 在python csv阅读器中包含空字节错误
- 23. EOF阅读C/C++
- 24. 阅读包装线
- 25. 阅读从C#
- 26. 阅读C#
- 27. 阅读C#
- 28. 阅读C#
- 29. 如何阅读控制在c + +中的Debian软件包?
- 30. 在Lua中包含C库
会将文件包含*只有*号,也可能有其他字符? – 2009-12-18 12:22:58
您的问题似乎与您的示例无关。数字不是特殊字符... – James 2009-12-18 12:37:19