views:

155

answers:

3

Read a Text file having any line starts from "//" omit this line and moved to next line. The Input text file having some seprate partitions. Find line by line process and this mark.

A: 
Class SplLineIgnorStrmReader:StreamReader  // derived class from StreamReader

SplLineIgnorStrmReader ConverterDefFileReadStream = null;
{
//created the Obj for this Class.
Obj = new SplLineIgnorStrmReader(strFile, Encoding.default);
}

public override string ReadLine()
        {
            string strLineText = "", strTemp;
            while (!EndOfStream)
            {
                strLineText = base.ReadLine();
                strLineText = strLineText.TrimStart(' ');
                strLineText = strLineText.TrimEnd(' ');
                strTemp = strLineText.Substring(0, 2);
                if (strTemp == "//")
                    continue;
                break;

            }
            return strLineText;

This is if u want to read the Text file and omit any comments from that file(here exclude "//" comment).

Rajan
Could you format this as 'code' so it's actually readable?
axel_c
A: 

I'm not sure what you exactly need but, if you just want to filter out // lines from some text in a stream... just remember to close the stream after using it.

public string FilterComments(System.IO.Stream stream)
        {
            var data = new System.Text.StringBuilder();
            using (var reader = new System.IO.StreamReader(stream))
            {
                var line = string.Empty;
                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    if (!line.TrimStart(' ').StartsWith("//"))
                    {
                        data.Append(line);
                    }
                }
            }

            return data.ToString();
        }
RonaldV
Btw use the type instead of var when using C# 2.0.
RonaldV
A: 

If you are using .Net 3.5 you can use LINQ with a IEnumerable wrapped around a Stream Reader. This cool part if then you can just use a where statement to file statmens or better yet use a select with a regular expression to just trim the comment and leave data on the same line.

//.Net 3.5
static class Program
{
    static void Main(string[] args)
    {
        var clean = from line in args[0].ReadAsLines()
                    let trimmed = line.Trim()
                    where !trimmed.StartsWith("//")
                    select line;
    }
    static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}

...

//.Net 2.0
static class Program
{
    static void Main(string[] args)
    {
        var clean = FilteredLines(args[0]);
    }
    static IEnumerable<string> FilteredLines(string filename)
    {
        foreach (var line in ReadAsLines(filename))
            if (line.TrimStart().StartsWith("//"))
                yield return line;
    }
    static IEnumerable<string> ReadAsLines(string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}
Matthew Whited