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

问题描述 投票:0回答:1

好吧,我到处搜索了,所以找不到手..

  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;
    }
c++ string string-comparison
1个回答
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;
}

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

© www.soinside.com 2019 - 2024. All rights reserved.