CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    [RESOLVED] Comparing and Updating Text Files

    I'm having a problem wrapping my head around something...

    I need to have a piece of code that will compare two text files (technically .ini files, but same idea) and then update FileA with any data it's missing from FileB.

    Example:
    FileA has 3 lines
    Code:
    Bob
    Jim
    George
    FileB has 5 lines
    Code:
    David
    Susan
    Jack
    Jim
    Bob
    I need to update FileA with the names it is missing without removing any that it already has (or create a new file with combined information but no duplicates).

    For some reason, I cannot figure this out without making some really ugly code. (Which hasn't worked right either)

    Any help is greatly appreciated!

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Comparing and Updating Text Files

    this code works, though I am sure there is a better way of doing it...

    Code:
    TextReader fileA = new StreamReader(@"C:\Test\FileA.txt");
    
    StringBuilder fileBNewText = new StringBuilder();
    
    while (fileA.Peek() >= 0)
    {
        string lineA = fileA.ReadLine();
    
        if (!string.IsNullOrEmpty(lineA))
        {
            bool found = false;
    
            TextReader fileB = new StreamReader(@"C:\Test\FileB.txt");
    
            while (fileB.Peek() >= 0)
            {
                string lineB = fileB.ReadLine();
    
                if (lineA == lineB)
                {
                    found = true;
                    break;
                }
            }
    
            if (!found)
                fileBNewText.AppendLine(lineA);
    
            fileB.Close();
            fileB.Dispose();
        }
    }
    
    fileA.Close();
    fileA.Dispose();
    
    TextWriter fileBout = new StreamWriter(@"C:\Test\FileB.txt", true);
    fileBout.Write(fileBNewText.ToString(0, fileBNewText.Length - 2));
    fileBout.Close();
    fileBout.Dispose();
    
    TextReader fileB2 = new StreamReader(@"C:\Test\FileB.txt");
    StringBuilder fileANewText = new StringBuilder();
    
    while (fileB2.Peek() >= 0)
    {
        string lineB = fileB2.ReadLine();
    
        if (!string.IsNullOrEmpty(lineB))
        {
            bool found = false;
    
            TextReader fileA2 = new StreamReader(@"C:\Test\FileA.txt");
    
            while (fileA2.Peek() >= 0)
            {
                string lineA = fileA2.ReadLine();
    
                if (lineB == lineA || string.IsNullOrEmpty(lineB))
                {
                    found = true;
                    break;
                }
            }
    
            if (!found)
                fileANewText.AppendLine(lineB);
    
            fileA2.Close();
            fileA2.Dispose();
        }
    }
    
    fileB2.Close();
    fileB2.Dispose();
    
    TextWriter fileAout = new StreamWriter(@"C:\Test\FileA.txt", true);
    fileAout.Write(fileANewText.ToString(0, fileANewText.Length -2));
    fileAout.Close();
    fileAout.Dispose();

  3. #3
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    Re: Comparing and Updating Text Files

    Thanks eclipsed!

    With some editing and additions, I was able to get it working perfectly. Just couldn't get the basis down

    Cheers mate!

  4. #4
    Join Date
    Feb 2009
    Posts
    5

    Re: [RESOLVED] Comparing and Updating Text Files

    If it can be got down, keep it up but I need to show people this
    an other easier way
    Readline function can help read each line into a string collenction
    you have 2 collections that you can run 2 loops to compare


    5BabyBulls!

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: [RESOLVED] Comparing and Updating Text Files

    Quote Originally Posted by 5BabyBulls View Post
    If it can be got down, keep it up but I need to show people this
    an other easier way
    Readline function can help read each line into a string collenction
    you have 2 collections that you can run 2 loops to compare


    5BabyBulls!
    care to show code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured