How can I remove the last line from a text file when writing linest to the file using streamwriter ? and how to compare two text files with dates ?

rhodanny 166 Reputation points
2021-12-26T19:33:31.763+00:00
StreamWriter w = new StreamWriter(folders[0] + "\\dates.txt", true);  
            for (int i = 0; i < dates.Count; i++)  
            {  
  
                w.WriteLine(dates[i]);  
            }  
  
            w.Close();  

I think there is a space empty line at the bottom I can move down and then make backspace to delete the last empty line.

160508-dates1.jpg

The problem is later I want to read this dates in the text file with another text file of dates and make a comparison between them.
The first date in one file against all dates in the other file and so on and if there is a duplicated dates to remove them from one of the lists.

Developer technologies Windows Forms
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Ken Tucker 5,861 Reputation points
    2021-12-26T19:56:30.947+00:00

    Assuming dates is a string[]

             for (int i = 0; i < dates.Count; i++)
             {
                 if(!string.IsNullOrEmpty(dates[i])
                 {
                      w.WriteLine(dates[i]);
                 }
             }
    
             w.Close();
    
    1 person found this answer helpful.

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-12-26T21:01:14.117+00:00

    See if this works for you. The last foreach, rather then using Debug.WriteLine you could add the dates to a list for final results.

    List<DateTime> results1 = File
        .ReadAllLines("dates.txt")
        .Where(line => !string.IsNullOrWhiteSpace(line))
        .Select(line => DateTime.ParseExact(line, "dd/MM/yyyy HH:mm:ss", 
            CultureInfo.InvariantCulture)).ToList();
    
    List<DateTime> results2 = File
        .ReadAllLines("dates1.txt")
        .Where(line => !string.IsNullOrWhiteSpace(line))
        .Select(line => DateTime.ParseExact(line, "dd/MM/yyyy HH:mm:ss", 
            CultureInfo.InvariantCulture))
        .ToList();
    
    
    List<DateTime> duplicates = results1.Intersect(results2).ToList();
    
    if (duplicates.Any())
    {
        foreach (var duplicate in duplicates)
        {
            Debug.WriteLine(duplicate);
        }
    }
    
    Debug.WriteLine("");
    
    List<DateTime> except = results1.Except(results2).ToList();
    
    foreach (var dateTime in except)
    {
        Debug.WriteLine(dateTime);
    }
    
    Debug.WriteLine("");
    
    foreach (var dateTime in results1)
    {
        if (results2.Contains(dateTime))
        {
            Debug.WriteLine($"{dateTime} is a dup");
        }
        else
        {
            Debug.WriteLine($"{dateTime} is not a dup");
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

  3. WayneAKing 4,931 Reputation points
    2021-12-27T07:06:36.87+00:00

    Regarding the "blank line" at the end of the file, what
    you are seeing is likely the result of WriteLine always
    adding a newline at the end of the line it writes. That
    is how each DateTime written appears on a separate line.

    One way to avoid that is to use WriteLine for all lines
    except the last one. Use Write instead of WriteLine for
    the last item in the List.

    int i;
    for (i = 0; i < dates.Count - 1; i++)
    {
        w.WriteLine(dates[i]);
    }
    w.Write(dates[i]);
    
    w.Close();
    
    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.