开发者

C# skipping first line of a text file

here is the code i'm using

        using (FileStream fs = new FileStream(filename, FileMode.Open))
        using (StreamReader rdr = new StreamReader(fs))
        {
            while (!rdr.EndOfStream)
            {
                for (int z = 0; z < 2; z++)
                {

       开发者_如何学C             string[] lines = rdr.ReadLine().Split('|');
                    {
                        sb.AppendLine(";Re");
                        sb.AppendLine("@C PAMT " + lines[3]);
                        sb.AppendLine("@T " + lines[0]);
                        sb.AppendLine("@D @I\\" + lines[1]).Replace("I:\\", "");
                        sb.AppendLine(lines[2].Replace(";", "\r\n");
                    }
                }
            }
        }

        using (FileStream fs = new FileStream(outputfilename, FileMode.Create))
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.Write(sb.ToString());
        }

All i want is to either skip the first line of the StreamReader or skip the entire first stringbuilder. I thought the for would do it but it doesnt.

Note: i'd like to advoid a foreach.


Why not justn add the following line immediately after creating the rdr variable?

if ( !rdr.EndOfStream ) {rdr.ReadLine();}


Why not just read the first line and do nothing with it?

using (StreamReader rdr = new StreamReader(fs))
{
  rdr.ReadLine();
  ...

or if you don't want to do that, have an outside variable i and test for i!=0


How about

if(z > 0)
{
  // All of your sb.Append stuff
}

EDIT:

using (FileStream fs = new FileStream(filename, FileMode.Open)) 
using (StreamReader rdr = new StreamReader(fs)) 
{  
    bool passedFirstline = false;
    while (!rdr.EndOfStream) 
    { 
        string[] lines = rdr.ReadLine().Split('|');
        if(passedFirstLine)                     
        { 
            sb.AppendLine(";Re"); 
            sb.AppendLine("@C PAMT " + lines[3]); 
            sb.AppendLine("@T " + lines[0]); 
            sb.AppendLine("@D @I\\" + lines[1]).Replace("I:\\", ""); 
            sb.AppendLine(lines[2].Replace(";", "\r\n"); 
        } 
        else
        {
            passedFirstLine = true;
        }
    } 
} 

using (FileStream fs = new FileStream(outputfilename, FileMode.Create)) 
using (StreamWriter writer = new StreamWriter(fs)) 
{ 
   writer.Write(sb.ToString()); 
}


If you want to use it more times in your program then it's maybe a good idea to make a custom class inherited from StreamReader with the ability to skip lines.

Something like this could do:

class SkippableStreamReader : StreamReader
{
    public SkippableStreamReader(string path) : base(path) { }

    public void SkipLines(int linecount)
    {
        for (int i = 0; i < linecount; i++)
        {
            this.ReadLine();
        }
    }
}

after this you could use the SkippableStreamReader's function to skip lines. Example:

SkippableStreamReader exampleReader = new SkippableStreamReader("file_to_read");

//do stuff
//and when needed
exampleReader.SkipLines(number_of_lines_to_skip);


I know that not everyone is a fan of using the continue keyword, but in cases like this I almost like it because of the readability. You could change the following code to use a continue:

string[] lines = rdr.ReadLine().Split('|');
{
    if (z == 0) continue;   // Skip first line

    sb.AppendLine(";Re");
    sb.AppendLine("@C PAMT " + lines[3]);
    sb.AppendLine("@T " + lines[0]);
    sb.AppendLine("@D @I\\" + lines[1]).Replace(@"I:\", "");
    sb.AppendLine(lines[2].Replace(";", "\r\n");
}


using (FileStream fs = new FileStream(filename, FileMode.Open)) 
using (StreamReader rdr = new StreamReader(fs)) { 
  rdr.ReadLine(); // skip the first line
  string line = null;
  while ((line = rdr.ReadLine()) != null) {
    string[] lines = line.Split('|');
    sb.AppendLine(";Re"); 
    sb.AppendLine("@C PAMT " + lines[3]); 
    sb.AppendLine("@T " + lines[0]); 
    sb.AppendLine("@D @I\\" + lines[1]).Replace("I:\\", ""); 
    sb.AppendLine(lines[2].Replace(";", "\r\n"); 
  }
} 

if (sb.Length == 0) return; // return if nothing was processed

using (FileStream fs = new FileStream(outputfilename, FileMode.Create)) 
using (StreamWriter writer = new StreamWriter(fs)) 
{ 
    writer.Write(sb.ToString()); 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜