gpt4 book ai didi

c++ - 如何在C++中比较多个.txt文件?

转载 作者:行者123 更新时间:2023-12-02 10:22:57 25 4
gpt4 key购买 nike

我制作了一个程序,人们可以在其中创建一个拍卖,然后将其保存为如下所示的.txt文件:

Auction name: Books
Item to sell: Lord of the rings
Description: words........
Reserve price: 12

然后,在一个单独的项目中,我进行了类似的操作,但出价也被保存在具有以下结构的.txt文件中:

// "personName.txt"
24 ( <-- Example number of the person's bid)

问题是,现在我必须制作多个“出价”文件,并将它们与拍卖文件相互比较,以确定谁赢得了拍卖。

如何做到这一点?

最佳答案

我不确定为什么要对文件执行此任务,因为将文件加载到程序中非常昂贵。但是,您的任务可以完成(也就是说,如果我准确地理解了您的要求;您的问题并未写得很清楚)。

首先,请确保包含来自STL的适当 header

#include <iostream>    // Assuming you have console IO functionality
#include <filesystem> // Accessing all files in a directory for searching
#include <fstream> // Loading files into the program
#include <string> // Assuming you have names for bidders
#include <vector> // Load the files' contents into a vector

正如您可能能够从包含项及其说明中看出的那样,这是我对该程序的计划:
  • 将所有拍卖文件存储在只有拍卖文件的目录中。这将节省时间,因为该程序不必检查我们不感兴趣的一堆文件。
  • 使用std::filesystem::directory_iterator从上方搜索目录中的所有文件,并将其内容保存到std::vector
  • 使用max函数确定最高出价并输出其名称

  • 实际上,这意味着我们可能需要一个结构 vector ,该结构具有(1)投标人的名称和(2)他们的投标。然后,我们使用max函数(如上所述)比较所有出价。当找到最高出价时,我们将能够非常容易地找到与该出价一起使用的名称出价者并将其输出到屏幕。
    .txt文件(对于我的解决方案)每个文件严格有两行。第一行是投标人的名称,第二行是投标人的出价。这将与我们用于将这些文件加载​​到程序中的结构完全一致。

    因此,这是程序(确保您使用的是 C++17):

    #include <filesystem> // std::filesystem : path, directory_iterator, current_path, parent_path
    #include <fstream> // std::ifstream
    #include <iomanip> // std::setpercision
    #include <iostream> // std::cout, std::cerr, std::endl
    #include <limits> // std::numeric_limits
    #include <string> // std::string
    #include <vector> // std::vector

    namespace fs = std::filesystem;

    // The max size of a file stream saved into this variables
    constexpr auto STREAM_MAX = std::numeric_limits<std::streamsize>::max();

    // The directory to the folder with all the bidders in it
    // What I've done here is used the a folder in the same parent
    // directory as this file. So we go one step out of the
    // current path, then go into the "bids" folder. However,
    // I compiled this in a different directory hierarchy, so I
    // commented the '.parent_path()' part out.
    const auto BIDDING_PATH =
    fs::path(fs::current_path()/*.parent_path()*/ / "bids");

    /**
    This will be the struct we use in the vector

    @param name The name of the bidder
    @param bid The price of the bidder's bid
    */
    struct Bidder
    {
    std::string name;
    double bid;

    // Define a constructor for std::vector::emplace_back
    Bidder(std::string name, double bid) :
    name(std::move(name)), bid(bid)
    {}
    };

    std::vector<Bidder> load_bids()
    {
    std::vector<Bidder> all_bidders;

    // Loop through every file in the 'BIDDING_PATH' directory
    // (not recursively)
    for (auto file : fs::directory_iterator(BIDDING_PATH))
    {
    std::ifstream cur_file(file.path().c_str());
    if (cur_file.is_open())
    { // Load file into a struct and add it to `all_bidders`
    std::string cur_name;
    double cur_bid;

    // Read the contents of the file into the above variables
    std::getline(cur_file, cur_name);
    cur_file >> cur_bid;

    // Now get rid of the newline character left behind by the '>>'
    // operator from 'cur_file >> cur_bid'
    cur_file.ignore(STREAM_MAX, '\n');

    // Add the bidder to the `all_bidders` vector
    all_bidders.emplace_back(cur_name, cur_bid);

    // Some debugging feedback
    std::cout
    << "File loaded: " << file.path().string() << '\n'
    << "Bidder's name: " << all_bidders.back().name << '\n'
    << "Bidder's bid: " << all_bidders.back().bid << '\n'
    << std::endl;
    }
    else
    {
    std::cerr
    << "Error opening file: " << file.path().string()
    << std::endl;
    }
    }

    return all_bidders;
    }

    Bidder find_max_bidder(const std::vector<Bidder>& bidders)
    {
    // Keeps track of the winning bidder
    // Intially set to a null value
    Bidder max_bidder("", -1.0);

    for (const auto& cur_bidder : bidders)
    {
    if (cur_bidder.bid > max_bidder.bid)
    {
    max_bidder.name = cur_bidder.name;
    max_bidder.bid = cur_bidder.bid;
    }
    }

    // Check if no max was found
    if (max_bidder.bid == -1.0)
    {
    std::cerr
    << "Error: No max bidder was found\n"
    << "Are there any bidding files in the given directory?\n"
    << std::endl;
    }

    return max_bidder;
    }

    int main()
    {
    // Load all bidders into the `all_bidders` vector, then
    // calculate the max bid
    std::vector<Bidder> all_bidders = load_bids();
    Bidder winning_bidder = find_max_bidder(all_bidders);

    // Print out the max bid
    std::cout
    << "The winner of this auction is " << winning_bidder.name << "!\n"
    << "They bid $" << winning_bidder.bid << std::setprecision(2) << "!\n"
    << std::endl;

    // Keep the console from closing
    std::cin.get();
    return 0;
    }

    请注意以下几点:
  • 您需要使用C++17进行编译;我将MSVC与Visual Studio 2019一起使用
  • 不需要其他#include来解释我们的游戏计划,但是我添加了
  • 来格式化或简化生活
  • 如果需要
  • ,则可以删除“ // Some debugging feedback”下的部分
  • 我没有涵盖您问题的第一部分,因为它(1)似乎并不像是一个问题,并且(2)您似乎对该部分没有任何担忧

  • 此外,我使用此批处理文件生成了一组随机数据:

    @echo off
    setlocal EnableDelayedExpansion
    set wkdir=!cd!
    goto :begin

    :begin
    cd "..\bids"
    set rand_bid=
    for /l %%a in (1,1,100) do (
    echo Bidder%%a > bidder%%a.txt
    set /a rand_bid=!random! %% 1000
    echo !rand_bid!.00 >> bidder%%a.txt
    )
    cd !wkdir!
    goto :end

    :end
    exit

    运行该批处理文件然后运行我的程序之后,这是我的输出:

    File loaded:   C:\Dev\C++\Others\DevBox\Testing\bids\bidder1.txt
    Bidder's name: Bidder1
    Bidder's bid: 219

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder10.txt
    Bidder's name: Bidder10
    Bidder's bid: 693

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder100.txt
    Bidder's name: Bidder100
    Bidder's bid: 275

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder11.txt
    Bidder's name: Bidder11
    Bidder's bid: 669

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder12.txt
    Bidder's name: Bidder12
    Bidder's bid: 720

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder13.txt
    Bidder's name: Bidder13
    Bidder's bid: 741

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder14.txt
    Bidder's name: Bidder14
    Bidder's bid: 342

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder15.txt
    Bidder's name: Bidder15
    Bidder's bid: 124

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder16.txt
    Bidder's name: Bidder16
    Bidder's bid: 627

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder17.txt
    Bidder's name: Bidder17
    Bidder's bid: 244

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder18.txt
    Bidder's name: Bidder18
    Bidder's bid: 61

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder19.txt
    Bidder's name: Bidder19
    Bidder's bid: 79

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder2.txt
    Bidder's name: Bidder2
    Bidder's bid: 979

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder20.txt
    Bidder's name: Bidder20
    Bidder's bid: 211

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder21.txt
    Bidder's name: Bidder21
    Bidder's bid: 168

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder22.txt
    Bidder's name: Bidder22
    Bidder's bid: 44

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder23.txt
    Bidder's name: Bidder23
    Bidder's bid: 902

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder24.txt
    Bidder's name: Bidder24
    Bidder's bid: 753

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder25.txt
    Bidder's name: Bidder25
    Bidder's bid: 975

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder26.txt
    Bidder's name: Bidder26
    Bidder's bid: 220

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder27.txt
    Bidder's name: Bidder27
    Bidder's bid: 576

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder28.txt
    Bidder's name: Bidder28
    Bidder's bid: 541

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder29.txt
    Bidder's name: Bidder29
    Bidder's bid: 249

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder3.txt
    Bidder's name: Bidder3
    Bidder's bid: 788

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder30.txt
    Bidder's name: Bidder30
    Bidder's bid: 661

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder31.txt
    Bidder's name: Bidder31
    Bidder's bid: 98

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder32.txt
    Bidder's name: Bidder32
    Bidder's bid: 307

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder33.txt
    Bidder's name: Bidder33
    Bidder's bid: 698

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder34.txt
    Bidder's name: Bidder34
    Bidder's bid: 287

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder35.txt
    Bidder's name: Bidder35
    Bidder's bid: 489

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder36.txt
    Bidder's name: Bidder36
    Bidder's bid: 685

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder37.txt
    Bidder's name: Bidder37
    Bidder's bid: 196

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder38.txt
    Bidder's name: Bidder38
    Bidder's bid: 321

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder39.txt
    Bidder's name: Bidder39
    Bidder's bid: 332

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder4.txt
    Bidder's name: Bidder4
    Bidder's bid: 267

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder40.txt
    Bidder's name: Bidder40
    Bidder's bid: 593

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder41.txt
    Bidder's name: Bidder41
    Bidder's bid: 485

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder42.txt
    Bidder's name: Bidder42
    Bidder's bid: 863

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder43.txt
    Bidder's name: Bidder43
    Bidder's bid: 869

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder44.txt
    Bidder's name: Bidder44
    Bidder's bid: 612

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder45.txt
    Bidder's name: Bidder45
    Bidder's bid: 165

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder46.txt
    Bidder's name: Bidder46
    Bidder's bid: 889

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder47.txt
    Bidder's name: Bidder47
    Bidder's bid: 554

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder48.txt
    Bidder's name: Bidder48
    Bidder's bid: 783

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder49.txt
    Bidder's name: Bidder49
    Bidder's bid: 747

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder5.txt
    Bidder's name: Bidder5
    Bidder's bid: 966

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder50.txt
    Bidder's name: Bidder50
    Bidder's bid: 322

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder51.txt
    Bidder's name: Bidder51
    Bidder's bid: 775

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder52.txt
    Bidder's name: Bidder52
    Bidder's bid: 306

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder53.txt
    Bidder's name: Bidder53
    Bidder's bid: 376

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder54.txt
    Bidder's name: Bidder54
    Bidder's bid: 895

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder55.txt
    Bidder's name: Bidder55
    Bidder's bid: 374

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder56.txt
    Bidder's name: Bidder56
    Bidder's bid: 166

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder57.txt
    Bidder's name: Bidder57
    Bidder's bid: 332

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder58.txt
    Bidder's name: Bidder58
    Bidder's bid: 665

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder59.txt
    Bidder's name: Bidder59
    Bidder's bid: 177

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder6.txt
    Bidder's name: Bidder6
    Bidder's bid: 888

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder60.txt
    Bidder's name: Bidder60
    Bidder's bid: 399

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder61.txt
    Bidder's name: Bidder61
    Bidder's bid: 271

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder62.txt
    Bidder's name: Bidder62
    Bidder's bid: 996

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder63.txt
    Bidder's name: Bidder63
    Bidder's bid: 487

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder64.txt
    Bidder's name: Bidder64
    Bidder's bid: 874

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder65.txt
    Bidder's name: Bidder65
    Bidder's bid: 499

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder66.txt
    Bidder's name: Bidder66
    Bidder's bid: 72

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder67.txt
    Bidder's name: Bidder67
    Bidder's bid: 902

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder68.txt
    Bidder's name: Bidder68
    Bidder's bid: 66

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder69.txt
    Bidder's name: Bidder69
    Bidder's bid: 64

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder7.txt
    Bidder's name: Bidder7
    Bidder's bid: 246

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder70.txt
    Bidder's name: Bidder70
    Bidder's bid: 167

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder71.txt
    Bidder's name: Bidder71
    Bidder's bid: 961

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder72.txt
    Bidder's name: Bidder72
    Bidder's bid: 250

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder73.txt
    Bidder's name: Bidder73
    Bidder's bid: 280

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder74.txt
    Bidder's name: Bidder74
    Bidder's bid: 185

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder75.txt
    Bidder's name: Bidder75
    Bidder's bid: 962

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder76.txt
    Bidder's name: Bidder76
    Bidder's bid: 174

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder77.txt
    Bidder's name: Bidder77
    Bidder's bid: 157

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder78.txt
    Bidder's name: Bidder78
    Bidder's bid: 221

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder79.txt
    Bidder's name: Bidder79
    Bidder's bid: 312

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder8.txt
    Bidder's name: Bidder8
    Bidder's bid: 189

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder80.txt
    Bidder's name: Bidder80
    Bidder's bid: 842

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder81.txt
    Bidder's name: Bidder81
    Bidder's bid: 415

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder82.txt
    Bidder's name: Bidder82
    Bidder's bid: 313

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder83.txt
    Bidder's name: Bidder83
    Bidder's bid: 220

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder84.txt
    Bidder's name: Bidder84
    Bidder's bid: 200

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder85.txt
    Bidder's name: Bidder85
    Bidder's bid: 738

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder86.txt
    Bidder's name: Bidder86
    Bidder's bid: 323

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder87.txt
    Bidder's name: Bidder87
    Bidder's bid: 529

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder88.txt
    Bidder's name: Bidder88
    Bidder's bid: 196

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder89.txt
    Bidder's name: Bidder89
    Bidder's bid: 991

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder9.txt
    Bidder's name: Bidder9
    Bidder's bid: 917

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder90.txt
    Bidder's name: Bidder90
    Bidder's bid: 441

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder91.txt
    Bidder's name: Bidder91
    Bidder's bid: 691

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder92.txt
    Bidder's name: Bidder92
    Bidder's bid: 578

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder93.txt
    Bidder's name: Bidder93
    Bidder's bid: 234

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder94.txt
    Bidder's name: Bidder94
    Bidder's bid: 638

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder95.txt
    Bidder's name: Bidder95
    Bidder's bid: 569

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder96.txt
    Bidder's name: Bidder96
    Bidder's bid: 267

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder97.txt
    Bidder's name: Bidder97
    Bidder's bid: 788

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder98.txt
    Bidder's name: Bidder98
    Bidder's bid: 790

    File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder99.txt
    Bidder's name: Bidder99
    Bidder's bid: 781

    The winner of this auction is Bidder62 !
    They bid $996!

    希望对您有所帮助!如果你有任何问题随时问!

    关于c++ - 如何在C++中比较多个.txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59259277/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com