Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取字符串中的每个数字c#_C#_List - Fatal编程技术网

读取字符串中的每个数字c#

读取字符串中的每个数字c#,c#,list,C#,List,假设这是我的txt文件: line1 line2 line3 line4 line5 im正在使用以下命令读取此文件的内容: string line; List<string> stdList = new List<string>(); StreamReader file = new StreamReader(myfile); while ((line = file.ReadLine()) != null) { stdList

假设这是我的txt文件:

line1
line2
line3
line4
line5
im正在使用以下命令读取此文件的内容:

 string line;
List<string> stdList = new List<string>();

StreamReader file = new StreamReader(myfile);
while ((line = file.ReadLine()) != null)
{                
    stdList.Add(line);           
}
finally
{//need help here
}
字符串行;
List stdList=新列表();
StreamReader文件=新的StreamReader(myfile);
而((line=file.ReadLine())!=null)
{                
添加标准列表(行);
}
最后
{//这里需要帮助吗
}
现在我想读取stdList中的数据,但每2行读取一个只读值(在本例中,我必须读取“line2”和“line4”)。
有人能给我提供正确的方法吗?

在循环中添加条件块和跟踪机制。(循环的主体如下所示:)

linesProcessed%2==1
表示:获取我们已经处理的行数,并找到该数字的
mod 2
。(整数除以2时的余数。)这将检查处理的行数是偶数还是奇数

如果您没有处理任何行,它将被跳过(如第1行,您的第一行)。如果您已经处理了一行或任意奇数行,请继续处理当前行(如第2行)


如果模块化数学给您带来了任何麻烦,请参见以下问题:

您可以将文件读取逻辑简化为一行,然后以这种方式每隔一行循环一次:

var lines = File.ReadAllLines(myFile);
for (var i = 1; i < lines.Length; i += 2) {
  // do something
}
var lines=File.ReadAllLines(myFile);
对于(变量i=1;i
编辑:从示例中的
i=1开始,即
line2

尝试以下操作:

string line;
List<string> stdList = new List<string>();

StreamReader file = new StreamReader(myfile);
while ((line = file.ReadLine()) != null)
{
    stdList.Add(line);
    var trash = file.ReadLine();  //this advances to the next line, and doesn't do anything with the result
}
finally
{
}
字符串行;
List stdList=新列表();
StreamReader文件=新的StreamReader(myfile);
而((line=file.ReadLine())!=null)
{
添加标准列表(行);
var trash=file.ReadLine();//这将前进到下一行,对结果没有任何影响
}
最后
{
}

甚至比Yuck的方法更短,而且不需要一次性将整个文件读入内存:)


诚然,它确实需要.NET4。关键部分是提供索引以及谓词作用的值的。我们并不真正关心值(这就是为什么我将参数命名为“忽略”
)-我们只需要奇数索引。显然,当我们构建列表时,我们关心值,但这很好-它只对谓词被忽略。

Bah,我更喜欢你的答案+1视情况而定-
ReadAllLines
对大文件有问题,它会将整个文件存储在内存中
ReadLine()
很懒,而且(理论上)可以处理任何大小的文件…@恶心:是的,但是你真的不需要显式循环。。。请参阅我的答案:)如果我使用i++that works(并读取oll行),如果我使用i+=2,我会得到错误,它是“仅添加第一行”。+1,因为它不需要您先将整个文件读入字符串。@devilkw我重命名了该变量以使其更有意义。现在已对其进行
行处理
。我还用澄清更新了我的答案。@Blackplant:为了简洁起见,我偷偷地在一行上把所有的内容都写了出来-但是,是的,这一点肯定更清楚:)@JonSkeet确实很好。您可能需要解释
忽略的
参数的作用以及它实际上被忽略的原因;)是的!LINQ扩展很漂亮,但是如果你不习惯使用lambdas,语法可能会很复杂。这个解释对我这样的人很有帮助。我最近刚开始使用lambdas。我只阅读第一行…所有其他行都被忽略,不会添加到列表中。什么是var垃圾?你能解释一下你的代码吗?在你的代码中,我只看到向var垃圾添加一个值。怎么用?你不会用的。对不起,我应该解释的
file.Readline()
从源代码中读取一行并前进到下一行。现在,你是否真的对它读到的行做了任何事情取决于你。你不需要它,因此你不需要垃圾这个名字。允许说:[语句开头]
file.ReadLine()–不使用方法调用的返回值。@JeppeStigNielsen是的,我知道。我只是想澄清一下,这条线实际上什么都没做。我猜它有点反作用多亏了所有人的回答,我用了这个。
string line;
List<string> stdList = new List<string>();

StreamReader file = new StreamReader(myfile);
while ((line = file.ReadLine()) != null)
{
    stdList.Add(line);
    var trash = file.ReadLine();  //this advances to the next line, and doesn't do anything with the result
}
finally
{
}
var list = File.ReadLines(filename)
               .Where((ignored, index) => index % 2 == 1)
               .ToList();