5

suppose this is my txt file:

line1
line2
line3
line4
line5

im reading content of this file with:

 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
}

Now i want to read data in stdList, but read only value every 2 line(in this case i've to read "line2" and "line4"). can anyone put me in the right way?

mdm
  • 12,480
  • 5
  • 34
  • 53
devilkkw
  • 418
  • 2
  • 6
  • 17

4 Answers4

10

Even shorter than Yuck's approach and it doesn't need to read the whole file into memory in one go :)

var list = File.ReadLines(filename)
               .Where((ignored, index) => index % 2 == 1)
               .ToList();

Admittedly it does require .NET 4. The key part is the overload of Where which provides the index as well as the value for the predicate to act on. We don't really care about the value (which is why I've named the parameter ignored) - we just want odd indexes. Obviously we care about the value when we build the list, but that's fine - it's only ignored for the predicate.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @BlackVegetable: I was sneakily getting it all on one line for brevity - but yes, this is definitely clearer :) – Jon Skeet Aug 01 '12 at 16:50
  • @JonSkeet Very nice indeed. You might want to explain what the `ignored` parameter does and why it is, in fact, *ignored* ;) – Yuck Aug 01 '12 at 16:52
  • Yep! LINQ extensions are beautiful but the syntax can be tricky if you're not used to working with lambdas. – Yuck Aug 01 '12 at 16:54
  • The explanation is very helpful for someone like myself. I've just recently began working with lambdas. – Dan Aug 01 '12 at 17:02
  • i read only frist line...all other lines is ignored and not added to list. – devilkkw Aug 01 '12 at 17:52
7

You can simplify your file read logic into one line, and just loop through every other line this way:

var lines = File.ReadAllLines(myFile);
for (var i = 1; i < lines.Length; i += 2) {
  // do something
}

EDIT: Starting at i = 1 which is line2 in your example.

Yuck
  • 49,664
  • 13
  • 105
  • 135
5

Add a conditional block and a tracking mechanism inside of a loop. (The body of the loop is as follows:)

int linesProcessed = 0;
if( linesProcessed % 2 == 1 ){
  // Read the line.
  stdList.Add(line);
}
else{
  // Don't read the line (Do nothing.)
}
linesProcessed++;

The line linesProcessed % 2 == 1 says: take the number of lines we have processed already, and find the mod 2 of this number. (The remainder when you divide that integer by 2.) That will check to see if the number of lines processed is even or odd.

If you have processed no lines, it will be skipped (such as line 1, your first line.) If you have processed one line or any odd number of lines already, go ahead and process this current line (such as line 2.)

If modular math gives you any trouble, see the question: https://stackoverflow.com/a/90247/758446

Community
  • 1
  • 1
BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
0

try this:

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
{
}
Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
  • what mean var trash?can you explane your code?in your code i view only adding a value to var trash. how to use? – devilkkw Aug 01 '12 at 18:04
  • you won't use it. Sorry, I should have explained. `file.Readline()` reads a line from the source and advances to the next line. Now, whether you actually do anything with the line it read is up to you. You don't need it, thus the name "trash" – Phillip Schmidt Aug 01 '12 at 18:07
  • You don't need `trash`. It's allowed to say: [beginning of statement] `file.ReadLine();` – without using the return value of the method call. – Jeppe Stig Nielsen Aug 01 '12 at 18:40
  • @JeppeStigNielsen yeah, i know. I just wanted to kind of clarify that the line wasn't actually doing anything. Guess it sort of had a reverse effect – Phillip Schmidt Aug 01 '12 at 18:42
  • THANKS TO ALL FOR ANSWER,I USED THIS. – devilkkw Aug 01 '12 at 23:17