Heba
Heba

Reputation: 31

How do i compare two text files in c++

okay I've searched everywhere and couldn't get my hand on it so ..

  1. i'm doing a library system where a librarian enters his username and the program checks if he is one of the librarians or not
  2. i'm stuck on the comparing part , i tried using getline but it gave me an error , tried gets_s and used a char array instead of a string and still didn't work

kindly help me with what i should do

using namespace std;
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    //opening files
    ifstream readUsername;
    ofstream enterUsername;
    //variables
    string existUsername;
    string enteredUsername;
    //reading files
    readUsername.open("librarian usernames.txt");
    if (readUsername.fail())
    {
        cout << "can't open file" << endl;
    }
    enterUsername.open("entered librarian username.txt");
    if (enterUsername.fail())
    {
        cout << "can't open file" << endl;
    }
    while(!readUsername.eof)
    {       
        readUsername >> existUsername;
    }
    enterUsername << enteredUsername;

    readUsername.close();
    enterUsername.close();
    enterUsername.clear();

    system("pause");
    return 0;
    }

Upvotes: 2

Views: 2713

Answers (1)

Riley Worstell
Riley Worstell

Reputation: 66

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream infile;
infile.open("listOfWords.txt"); //open file

for(string listOfWords; getline(infile, listOfWords, '.'); ) //read sentences including
//spaces

cout<<listOfWords; //this displays

return 0;
}

This shows you how to output the text so you should just save both files to a variable then compare the variables.

Upvotes: 1

Related Questions