Write a program in c++ that read two files A.txt and B.txt, and find all words which are in file A.txt but not in file B.txt. Input: A file containing large text. The file may contain any number of words.

Recommended Answers

All 2 Replies

There are many different ways to do this. Perhaps, the simplest is this:

#include <fstream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>
#include <iostream>

template< typename T > std::set<T> file_to_set( const char* path )
{
    std::ifstream file(path) ;
    return std::set<T>( std::istream_iterator<T>(file), std::istream_iterator<T>() ) ;
}

int main()
{
    const std::set< std::string> a = file_to_set< std::string >( "a.txt" ) ;
    const std::set< std::string> b = file_to_set< std::string >( "b.txt" ) ;
    std::set_difference( a.begin(), a.end(), b.begin(), b.end(),
                         std::ostream_iterator< std::string >( std::cout, "\n" ) ) ;
}

@Karan : We are not here to do someone's homework. Put in your effort first...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.