Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am reading the content of log file using File.ReadAllLines like below

C#
fileContents = File.ReadAllLines( dataLogFile);


There are some commented lines on log file like this:

date Mon Aug 19 05:41:35 pm 2013
base hex timestamps absolute
no internal events logged
// version 7.6.0
// PGN=EF00 SA=B DA=FA
157.692673 1 18EFFA0Bx Rx d 8 01 00 13 DC 00 00 00 00 Length = 282000 BitCount = 145
// PGN=EF00 SA=B DA=FA
157.693679 1 18EFFA0Bx Rx d 8 02 00 00 00 00 00 00 00 Length = 286000 BitCount = 147
// PGN=F004 SA=0 DA=FF EEC1_E
157.693954 1 CF00400x Rx d 8 F4 A8 A9 92 23 FF FF A8 Length = 266000 BitCount = 137
// PGN=EF00 SA=B DA=FA
157.694670 1 18EFFA0Bx Rx d 8 02 00 09 FB 00 00 00 00 Length = 280000 BitCount = 144
// PGN=0 SA=B DA=0 TSC1_100_ABM_REX


I cannot read the commented lines of log file using File.ReadAllLines

I need to read that commented lines for filtering the data

Can anyone suggest me something about this

Thanks
John
Posted
Updated 19-Dec-13 4:00am
v2
Comments
BillWoodruff 19-Dec-13 10:09am    
Are you saying that: after executing your code to read all the lines, that lines beginning with "//" are not present in the result ?
Christopher Drake 19-Dec-13 10:25am    
There are no visible escape characters. You should be able to use ReadAllLines()

ReadAllLines will return all the text in the file. Period. It makes no ditintion about what you think is a comment. There are no such things as comments to File.ReadAllLines.

So, that brings us to the problem being in your procoessing of the file. But, we have no Idea what that code looks like so it's impossible to say what's wrong there.
 
Share this answer
 
Hi John,

While reading, try to replace // with white space then read the line.
Is it possible to do that?
I dont know just am guessing.

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
Hi Try like this..


C#
var fileContents = File.ReadAllLines(dataLogFile);
        var commentLines = fileContents.Where(k => k.StartsWith("//")).ToArray();
 
Share this answer
 
Comments
Member 10408451 22-Dec-13 10:53am    
Hi,
I tried this. But his code is reading only first line of my log file
I am getting something like this on console
// version 7.6.0
// Trigger number =1
Karthik_Mahalingam 22-Dec-13 22:49pm    
not clear. please explain ..
Member 10408451 22-Dec-13 22:55pm    
Hi,
fileContents = File.ReadAllLines(seclogPath1);
string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);

I am using the above lines of code to read the data and to split and save my data into string array.

But that data starts with "//" is not present in this

For example:
// PGN=EF00 SA=B DA=FA
How can I read this data???
Karthik_Mahalingam 22-Dec-13 23:38pm    
y u r using index ??
fileContents[Index] ???
try this if you wanted only commented line
C#
string [] my=   File.ReadAllLines(@"Path of file");

         foreach (string item in my)
         {
             if (item.StartsWith("//"))
             {
                 MessageBox.Show(item);
             }
         }
 
Share this answer
 
From http://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx
C#
using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string[] createText = { "Hello", "And", "Welcome" };
            File.WriteAllLines(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string[] readText = File.ReadAllLines(path);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}
 
Share this answer
 
Comments
Member 10408451 22-Dec-13 22:01pm    
Hi,
This code is working fine, I can see all the lines of data on Console.
But how can I make use of the information in commented lines
Christopher Drake 23-Dec-13 8:36am    
What do you want to do? If you want to do something specifically with the commented lines, you should handle each line separately.

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