Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a text file that contains some info in lines. I wanna read it by passing over some lines. For example, let's say i have 1-10 in the lines. When i'm reading i want to read it in the following way

1 <- i wanna read this
2 <- Skip this
3 <- read this
4 <- Skip this
5 <- read this
6 <- Skip this
7 <- read this
8 <- Skip this
9 <- read this
10 <- Skip this

you get the pattern right? how can i achieve this using c#? and i wanna get the lines i skipped at a later time too. Any ideas?
Posted

Because you can't really know how long is each line, there is no easy way to skip a line without reading it first.
Typically you'll have to read all the lines but discard the 2 even lines.

C#
string filePath = @"c:\windows\win.ini";

using (var fileReader = new System.IO.StreamReader(filePath))
{
    int lineNo = 1;
    string line;
    while (true)
    {
        line = fileReader.ReadLine();
        if (line == null) break; // we have reached the end of the file
        bool oddLine = ((lineNo % 2) == 1);
        if (oddLine) Console.WriteLine(lineNo + " " + line);
        lineNo++;
    }
}


This will output every other line:

1 ; for 16-bit app support
3 [extensions]
5 [files]
7 MAPI=1
9 CMC=1
11 MAPIXVER=1.0.0.1
13 [MCI Extensions.BAK]
15 3gp=MPEGVideo
17 3gpp=MPEGVideo
19 adt=MPEGVideo
21 m2t=MPEGVideo
23 m2v=MPEGVideo
 
Share this answer
 
You have the problem as stated above. However, I can give you an even easier way to readlines:

string[] lines = File.ReadLines(filePath);


See http://msdn.microsoft.com/en-us/library/dd383503.aspx[^]
 
Share this answer
 
Comments
Tim Corey 22-Jun-12 21:38pm    
Nice addition. +5
look this example:-
C#
private void ReadLinesNoFromFile(string FileNameWithPath)
        {
            string[] bt = null;
            if (System.IO.File.Exists(FileNameWithPath))
            {
                bt = System.IO.File.ReadAllLines(FileNameWithPath);
            }
string str =""
            for (int i = 0; i < bt.Length; i++)
            {
                if ((i+1 % 2)==1)
           str = str +i.ToString() + "<- read this" + bt[i].ToString();// read file line
        else
          str = str + i.ToString() + "<- Skip this" // no read file line

            }
        }
 
Share this answer
 
A variation and most effecient method to do the same:

C#
string filePath = @"c:\windows\win.ini";

using (var fileReader = new System.IO.StreamReader(filePath))
{
    int lineNo = 1;
    string line;
    while (true)
    {
        if(line % 2 ==0)
           {
               line = fileReader.ReadLine();
         }
    }
}
 
Share this answer
 
Comments
Sandeep Mewara 23-Jun-12 9:36am    
How is this an answer? your while loop will never end and it will go into infinite loop.
[no name] 23-Jun-12 14:26pm    
There is something called commonsense my friend, what you need to see there is line%2 == 0
Sandeep Mewara 23-Jun-12 16:12pm    
:) and you think that 'if' not executing will take execution out of while loop? Yeah... common-sense!
Richard MacCutchan 24-Jun-12 6:46am    
Take a look at his rant in Bugs & Sugs; it tells you a lot.
Richard MacCutchan 24-Jun-12 6:48am    
Even if this would compile it will not work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900