如何在 c++ 中比较两个文本文件

How do i compare two text files in c++

本文关键字:两个 文本 文件 c++ 比较      更新时间:2023-10-16

好吧,我到处搜索过,无法得到它,所以..

  1. 我正在做一个图书馆系统,图书馆员输入他的用户名,程序检查他是否是图书馆员之一
  2. 卡在比较部分,我尝试使用 getline 但它给了我一个错误,尝试gets_s并使用 char 数组而不是字符串,但仍然不起作用

请帮助我做什么

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;
}
#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;
}

这将向您展示如何输出文本,因此您应该将两个文件保存到一个变量中,然后比较变量。