Results 1 to 5 of 5

Thread: Algorithm or way to compare two txt files

  1. #1
    Join Date
    Jan 2014
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Algorithm or way to compare two txt files

    Hello. I searched a lot of algorithms, and I couldn't find that one I need

    I've got 2 txt file. In 1st I have:
    Qt Code:
    1. file1 176
    2. file2 209
    3. file3 159392
    4. file4 405617
    5. file5 405886
    6. file6 516485
    To copy to clipboard, switch view to plain text mode 
    And is 2nd:
    Qt Code:
    1. file1 176
    2. file4 405617
    3. file3 159392
    4. file8 49556
    5. file2 209
    To copy to clipboard, switch view to plain text mode 

    And I want to compare these files, to get only changes, so in this example it should be:
    Qt Code:
    1. file5 405886
    2. file6 516485
    3. file8 49556
    To copy to clipboard, switch view to plain text mode 

    1st part of line is name of file, and 2nd is size, so I can split them, put into array... anything.
    Only way I found to do it, is to put these files to array, take 1 line, and seek full 2nd array to find it, take next line and again. And vice versa.

    Is it possible to make it easier?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Algorithm or way to compare two txt files

    Easier? Probably not, your algorithm is about as simple as it can be.

    How many entries in the lists? A QHash will make the search faster for data sets of hundreds or more.
    Is order important in the output?
    Is there an implied or explicit order in the data?

  3. #3
    Join Date
    Jan 2014
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Algorithm or way to compare two txt files

    Quote Originally Posted by ChrisW67 View Post
    Easier? Probably not, your algorithm is about as simple as it can be.
    Oh sorry, I didn't mean "easier". I mean "more professional", because searching array, entry after entry, as I think, is not efficient and 'nice'. I thought that maybe exists any algorithm to do this things.

    Quote Originally Posted by ChrisW67 View Post
    How many entries in the lists? A QHash will make the search faster for data sets of hundreds or more.
    Is order important in the output?
    Is there an implied or explicit order in the data?
    How many... It should be universal, but probably between 1 to 1000 entries. Order is not important in output. I'm making a synchronization between PC and Network, so I must know which file should I upload/download.
    And last question: I don't know if I good understood it (any hidden data?), but there is no an implied or explicit order

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Algorithm or way to compare two txt files

    And I want to compare these files, to get only changes
    You can have 3 kinds of changes:

    1 - A file has been added on one side or the other
    2 - A file has been removed on one side or the other
    3 - A file is present on both sides, but the sizes are different

    Do you want to check for all 3 cases? If so, what should be the output in the case of a file that has been removed?

    One way to improve your current algorithm is to sort both lists first. Then you do not need to scan through all of list2 for each item on list1. You can make one pass through each of the lists by keeping track of where you are in each list.

    This discussion from stackoverflow describes what I mean.
    Last edited by d_stranz; 16th January 2014 at 04:50.

  5. #5
    Join Date
    Jan 2014
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Algorithm or way to compare two txt files

    Quote Originally Posted by d_stranz View Post
    You can have 3 kinds of changes:

    1 - A file has been added on one side or the other
    2 - A file has been removed on one side or the other
    3 - A file is present on both sides, but the sizes are different

    Do you want to check for all 3 cases? If so, what should be the output in the case of a file that has been removed?
    Yes, sth like that, but checking for modification of file I will try later. For removed files, I thought that that files just will be not showed in the list... But now I see that is bigger problem.
    I can have 2 (or more) clients in some PCs, management will be only with these clients. When I remove file from client 1, how to check in client 2, whether this file has been removed form client 1 and delete it, or if file has been added in client 2 and upload it...
    I want to do it like in dropbox :/ Any ideas? Leave other list in server with removed files..?

    Quote Originally Posted by d_stranz View Post
    One way to improve your current algorithm is to sort both lists first. Then you do not need to scan through all of list2 for each item on list1. You can make one pass through each of the lists by keeping track of where you are in each list.

    This discussion from stackoverflow describes what I mean.
    Thanks for that, that will be probably the best option to optimize.

Similar Threads

  1. Is ther any way to compare two text files
    By madhukiranmp in forum Newbie
    Replies: 5
    Last Post: 28th April 2015, 15:13
  2. A* algorithm
    By Ichi in forum Qt Programming
    Replies: 2
    Last Post: 31st March 2013, 18:31
  3. Help to set up sorting algorithm
    By Quasar in forum General Programming
    Replies: 2
    Last Post: 12th June 2012, 01:16
  4. Checksum generation (used to compare two or more files)
    By comlink21 in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2007, 09:28
  5. Dijkstra's Algorithm
    By therealjag in forum Qt Programming
    Replies: 2
    Last Post: 6th March 2006, 11:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.