1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C# compare 2 files contents for matches

Discussion in 'C, C++, C#' started by sandrine10, May 15, 2017.

  1. sandrine10

    sandrine10 Power Member

    Joined:
    Apr 14, 2010
    Messages:
    638
    Likes Received:
    65
    Gender:
    Female
    Location:
    FreeLand
    I am trying to find a way to compare some text in 2 files and if a match is found .

    Here are examples of the files;

    'File A'

    example1,textA,intA
    example2,textA,intA
    example3,textA,intA
    example4,textA,intA
    ...

    'File B'

    example1,textB,intB
    exampleA,textB,intB
    exampleB,textB,intB
    example4,textB,intB
    ...
    Using my 2 example files, I want to search them both and find matches(i mean example1 & example4)??!
     
  2. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    684
    Likes Received:
    326
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    load file1 as list
    load file2 as list

    then you can just loop through and see if match.

    foreach(var line in list1){
    if(list2.Contains(line)) {
    //got a match
    }
    }

    Or you could do some linq

    list1.Where(x=>list2.contains(x))
     
  3. sandrine10

    sandrine10 Power Member

    Joined:
    Apr 14, 2010
    Messages:
    638
    Likes Received:
    65
    Gender:
    Female
    Location:
    FreeLand
    Didn't work as i'm looking for matches substrings once found want write the whole line of each match found in fileA !
     
  4. chimpcoder

    chimpcoder Newbie

    Joined:
    Sep 13, 2016
    Messages:
    24
    Likes Received:
    3
    Gender:
    Male
    Do you want to compare line by line? I mean first line with first line and so on?