简体   繁体   中英

How would I detect the last two occurrence of characters/data sets in a text file c#

I have these text files as structured:

value1 value2 value3          
Table1 Table2 Table3
(data set values here)
.
.
.(represent text in between)
.
value1 value2 value3
Table1 Table2 Table3            
(data set values here)          

and

value1 value2 value3          
Table1 Table2 Table3
(data set values here)
.
.
.(represent text in between)
.
value1 value2 value3
Table1 Table2 Table3            
(data set values here)
.
.
. 
value1 value2 value3
Table1 Table2 Table3            
(data set values here)

As they are here, they are structured differently, with some having two Table sets, some having three, and others having 4. However, they all share a common characteristic that that they all have the same formatting of how the table is structured:

value1 value2 value3
Table1 Table2 Table3            
(data set values here)   

What I want to do is that I want to be able to skip to the last occurrence of each data set at the bottom, regardless of the number of lines in the file, which is the one shown above. How would I be able to detect the last values at the vary end?

What I have so far is:

string[] lines = File.ReadAllLines("myTextfile.txt"); //stores text file in array
foreach(var item in lines)
{
    Console.WriteLine(item.ToString()); //foreach loop here prints out code to console, line by line

}

I think I would have to use the LastIndexOf Method, then specify those values in the parameters, then use it in conjunction with the for loop but I am unsure how to implement it so it would detect it.

EDIT

Input of sample text files:

Time: 5:30am         
EndTime: 5:30pm.
.
.(represent text in between)
.
Time: 7:30am         
EndTime: 8:30pm           

Time: 6:30am         
EndTime: 1:30pm 
.
.
.(represent text in between)
.
Time: 8:30am         
EndTime: 9:30pm
.
.
. 
Time: 3:30am         
EndTime: 4:30pm

What I want to be able to is detect the last occurence of Time and EndTime, so when the program is run it will return:

//for the first one 
Time: 7:30am         
EndTime: 8:30pm 
//for the second one 
Time: 3:30am         
EndTime: 4:30pm

The simplest way to do the following:

  • Gather the last occurance of a line starting with "Time:"
  • Gather the last occurance of a line starting with "EndLine:"
  • Output the two as "Time: xxx" followed by "EndTime: xxx", with those two lines

would be something like this:

string time = "";
string endTime = "";
foreach (string line in File.ReadLines("filename.txt")) // or any source of strings
    if (line.StartsWith("Time:"))
        time = line;
    else if (line.StartsWith("EndTime:"))
        endTime = line;
// error handling would go here, like what if time is still empty? or endTime?
Console.WriteLine(time);
Console.WriteLine(endTime);

I'd build off the reading end of file question/answers on SO . So you could read a small buffer of lines to get the end ones. Something like this:

    static async Task Main(string[] args)
    {
        using (var reader = new StreamReader("foo.txt"))
        {
            var (start, end) = DecomposeLastTwoTimeLines(reader);

            Console.WriteLine($"Start: {start}");
            Console.WriteLine($"End: {end}");
        }
    }

    static ValueTuple<string, string> DecomposeLastTwoTimeLines(StreamReader reader)
    {
        long TimeLinesLength = ("EndTime: 12:30pm".Length * 2 * 2) + 2;

        if (reader.BaseStream.Length > TimeLinesLength)
        {
            reader.BaseStream.Seek(-TimeLinesLength, SeekOrigin.End);
        }

        List<string> lines = new List<string>();

        string line;
        while ((line = reader.ReadLine()) != null)
            lines.Add(line);

        if (lines.Count < 2)
            throw new FileFormatException();

        return (lines[^2], lines[^1]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM