Read multi-line txt file

Gomo

Member
Joined
Mar 9, 2017
Messages
7
Programming Experience
Beginner
Hello there fellas,

I was wondering if anyone could help me out with a small project of mine. In short, I'm trying to read a multi-line .csv file, and only add specific numbers from each line together. (basically I'm working with a log file) This is how it looks:
C#:
9/6/2017 11:48:40 PM,[COLOR=#ff0000]251[/COLOR],233
9/6/2017 11:48:45 PM,[COLOR=#ff0000]213[/COLOR],196
9/6/2017 11:48:50 PM,[COLOR=#ff0000]218[/COLOR],201
9/6/2017 11:48:55 PM,[COLOR=#ff0000]224[/COLOR],206

Software is adding 1 line every 5 seconds. So basically I'd like to open the file in C#, read all lines & add [22][23][24]'t element (if I counted it right :p.. red numbers) of each line and have it in the end save result in new file, with system date & time for name. Also, it should be 'resistant' to errors (there's a possibility that a line will contain only letters). It should also clear & save the original file once done.

Now, I know you guys aren't here to solve problems for others, at least not for free. So I'm offering to buy you a beer if you help me out with this. Your help would mean a lot! Thanks!

P.S Yes I did try to patch some code snippets on my own (found online) but those were only partially working, had missing functions, errors and were simply badly written.
 
I won't write all of your code but this should put you on the right track

C#:
int total = 0;
            using (var sr = new StreamReader("PathToFile"))
            {
                string line;
                string resultString = null;
                while ((line = sr.ReadLine()) != null)
                {
                    try
                    {
                        // I assumed the digits you are looking for would be between 1-6 in length, modify as needed
                        resultString = Regex.Match(line, @"(?<=[\d]{1,2}/[\d]{1,2}/[\d]{4} [\d]{1,2}:[\d]{1,2}:[\d]{1,2} (PM|AM),)[\d]{1,6}(?<=,[\d]{1,6})").Value;
                        bool isANumber = int.TryParse(line, out int result);
                        if (isANumber)
                        {
                            total += result;
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        // Handle Regex exception here
                    }
                    catch (Exception ex)
                    {
                        // Handle other exceptions (stream reader) here
                    }
                }
            }
 
I think the key here for you is to read in each line as a text file (easy to do and google how to do that).

Next, you need to .Split() each line using the comma as the splitter char. Something like this: string[] strData = line.Split(',');

This will then easily give you in strData[1] the data you are interested in.

I'm not clear on what you want to do with your data, but once you do it, its then a matter of saving it to a text file.

I would read in your data, modify it as you want, then store it either in an array string or a List<> string (I prefer the List<>) and finally use that structure to save to disk as a text file. Fairly easy to do and you can google for the code on how to do that.
 
Back
Top Bottom