C#读取所有行或某些行 [英] C# To read all lines or certain lines

查看:74
本文介绍了C#读取所有行或某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你知道为什么我有这个问题吗?



这两个代码之间的唯一区别是:

我占用所有行

我乘1行/ 10



其余代码在两者中都是相同的。





Do you know why I have this problem ?

The only difference between these two codes, is :
I take all lines
I take 1 line / 10

The rest of the code is the same in both.


data = new string[1000][];
            
int counter = 0;
while (line != null) {
counter++;
line = reader_trajectoires.ReadLine ();
                
/*if ((counter % 9) != 1) // To take all lines : Error message
continue;*/

string lines_data;
lines_data = line;
data [cpt] = lines_data.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries); // "Object reference not set to an instance of an object"

cpt ++;

}





而不是:





Instead of :

data = new string[1000][];
            
int counter = 0;
while (line != null) {
counter++;
line = reader_trajectoires.ReadLine ();
                
if ((counter % 9) != 1) // To take 1 line / 10 : No Error message
continue;

string lines_data;
lines_data = line;
data [cpt] = lines_data.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries);

cpt ++;

}





提前谢谢



Thank you in advance

推荐答案

你的行为是从根本上错误的。您正在尝试对某些数组的某些大小进行硬编码。这没有道理。如果文件比你预测的要大,那么你将耗尽数组索引,如果它更小,那你就会浪费内存。这只是一个很大的禁忌。数组总是可以动态分配。



首先,关于读取所有行。这不是太简单了吗?请参阅: https://msdn.microsoft .com / zh-cn / library / system.io.file.readalllines%28v = vs.110%29.aspx [ ^ ]。



另请参阅: https:// msdn.microsoft.com/en-us/library/System.IO.File%28v=vs.110%29.aspx [ ^ ]。



这解决了你的问题。现在,让我们谈谈添加成员的动态方法,如果事先不知道它们的数量。在这种情况下,请始终使用集合。您可以使用 System.Collections.Generic.List<> (适当的元素类型)并用数据填充它。最后,您始终可以将其转换为数组: https: //msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx [ ^ ]。



- SA
You are acting in a fundamentally wrong way. You are trying to hard-code some sizes of some arrays. It makes no sense. If the file is bigger then you predict, you will run out of array index, if it is smaller, you wast memory. It's just a big no-no. Arrays can always be allocated dynamically.

First of all, about reading all lines. Isn't that way too simple? Please see: https://msdn.microsoft.com/en-us/library/system.io.file.readalllines%28v=vs.110%29.aspx[^].

See also: https://msdn.microsoft.com/en-us/library/System.IO.File%28v=vs.110%29.aspx[^].

That solves your problem. Now, let's talk about dynamic ways of adding members if the number of them not known in advance. In such cases, always use collection. You can use System.Collections.Generic.List<> (of appropriate element type) and fill it with data. At the end of it, you can always convert it to an array: https://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx[^].

—SA


你在谈论多少行?

你可能超过你在<分配的1000个插槽分割所有行时code>数据当你有继续代码时,你使用1/10的行数。尝试使用集合而不是数组:

How many lines are you talking about?
The chances are that you are exceeding the 1000 "slots" that you have assigned in data when you Split all the lines - when you have the continue code in there you use 1/10 the number of lines. Try using a collection instead of an array:
List<string[]> data = new List<string[]>();

int counter = 0;
do
    {
    counter++;
    line = reader_trajectoires.ReadLine();
    if (line == null) break;

    data.Add(line.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries));
    } while (true);

请注意对也是循环。


你的变量为空。



您的执行顺序是:

1.检查行是否为空

2.然后读取行(现在可能为null)

3.然后使用它(如果值为null,它会引发空对象引用异常)



将检查空值调高一点,它应该没问题:

Your variable line is null.

Your order of execution is:
1. checking if line is not null
2. then reading line (now it could be null)
3. then using it (it raises null object reference exception if value is null)

Move checking for null a little bit higher and it should be fine:
while ( (line=reader_trajectoires.ReadLine()) != null)
{
    ....
}


这篇关于C#读取所有行或某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆