[英]How to read a text file and add the data to a int array in C#?
I'm trying to read a text file which contains numbers separated by a comma. 我正在尝试读取一个文本文件,其中包含用逗号分隔的数字。 When I read using File.Readline()
I get it to a string[]
. 当我使用File.Readline()
阅读时,将其获取到一个string[]
。 I need to convert it in to a int array but it gives an error. 我需要将其转换为一个int数组,但是会出现错误。
The contents of the text file: 文本文件的内容:
146429,143689,144380,141523,139572,136930,133714,130011,125843,121110,115974,110363,104367,97909,91245,84218,77008,69626,62058,54445,46942,39436,32146,24932,18359,12601,9039,9776,13638,18314,23221,27728,32142,35941,39577,42657,45692,48180 146429,143689,144380,141523,139572,136930,133714,130011,125843,121110,115974,110363,104367,97909,91245,84218,77008,69626,62058,54445,46942,39436,32146,24932,18359, 12601,9039,9776,13638,18314,23221,27728,32142,35941,39577,42657,45692,48180
My code: 我的代码:
while ((line = sr.ReadLine()) != null)
{
string[] values = line.Split(new string[] { " , " }, StringSplitOptions.None);
for (int i = 0; i < values.Length; i++)
{
// Console.WriteLine(values[i]);
valArr[LineCount][i] = Convert.ToInt64(values[i]); // error
}
LineCount++;
}
I think this is what you are after: 我认为这是您追求的目标:
static void Main(string[] args)
{
var sr = new StreamReader(@"d:\test.txt");
long[] data = ExtractData(sr).ToArray();
}
private static IEnumerable<long> ExtractData(StreamReader sr)
{
string line;
while ((line = sr.ReadLine()) != null)
{
var items = line.Split(',');
foreach (var item in items)
{
yield return Convert.ToInt64(item);
}
}
}
With my test file (d:\\test.txt) holding: 我的测试文件(d:\\ test.txt)保持在:
1,2,3,4,5
1,2,3,4
I get the array containing: 我得到包含的数组:
1,2,3,4,5,1,2,3,4
As Monroe pointed out, I missed the fact you wanted an array of arrays. 正如梦露指出的那样,我错过了您想要一个数组的事实。 Here's another version that gives such a jagged array. 这是另一个提供这种锯齿状数组的版本。 Still keeping yield in though ;) 虽然仍然保持产量;)
static void Main(string[] args)
{
var sr = new StreamReader(@"d:\test.txt");
var data = ExtractData(sr).ToArray();
}
private static IEnumerable<long[]> ExtractData(StreamReader sr)
{
string line;
while ((line = sr.ReadLine()) != null)
{
yield return line.Split(',').Select(i => Convert.ToInt64(i)).ToArray();
}
}
Using List
can help you, and use StringSplitOptions.RemoveEmptyEntries
to prevent null exception
in Convert.ToInt64
使用List
可以帮助您,并使用StringSplitOptions.RemoveEmptyEntries
来防止Convert.ToInt64
null exception
var lineArray = new List<List<Int64>>();
foreach (var lineString in File.ReadAllLines("path"))
{
var line = new List<Int64>();
string[] values = lineString.Split(new[] { ',', ' ' },
StringSplitOptions.RemoveEmptyEntries);
line.AddRange(values.Select(t => Convert.ToInt64(t)));
lineArray.Add(line);
}
and using it: 并使用它:
// Array of numbers for specific line
var resultArray = lineArray[lineNumber].ToArray();
It should be something nearly to: 应该接近:
int yourIntArray =
new List<string>(RealAllText(fileName)
.Split(new char[] {',', '\r', '\n', ' '},
stringSplitOptions.RemoveEmptyEntries))
.ConvertAll<int>(s => int.Parse(s))
.ToArray();
Of course, for more robustness, int.Parse
should be replaced with some method that will handle errors in numbers, and so on... 当然,为了获得更高的鲁棒性,应将int.Parse
替换为可处理数字错误的某种方法,以此类推...
EDIT: 编辑:
Oh, I just saw that you have lines that are another index to your array... Well then, this code should be modified in that case, but I'll leave that to you. 哦,我刚刚看到您的行是数组的另一个索引...那么,在这种情况下,应修改此代码,但我将留给您。
I think this is what you want to do. 我想这就是你想做的。 Only thing is that your vallArr should be a two dimensional array that can keep all values. 唯一的事情是您的vallArr应该是可以保留所有值的二维数组。 Something like var valArr = new Int64[x, y]; 像var valArr = new Int64 [x,y];
while ((line = sr.ReadLine()) != null)
{
string[] values = line.Split(new string[] { " , " }, StringSplitOptions.None);
for (int i = 0; i < values.Length; i++)
{
valArr[LineCount, i] = Int64.Parse(values[i]);
}
LineCount++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.