[英]How to compare multiple .txt files in c++?
I´ve made a program in which people create an auction which is then saved as a .txt file that looks like this:我制作了一个程序,人们在其中创建一个拍卖,然后将其保存为 .txt 文件,如下所示:
Auction name: Books
Item to sell: Lord of the rings
Description: words........
Reserve price: 12
Then, on a separate project, I made a similar thing but with Bids which are also saved on a .txt file with the structure:然后,在一个单独的项目中,我做了一个类似的事情,但投标也保存在一个具有以下结构的 .txt 文件中:
// "personName.txt"
24 ( <-- Example number of the person's bid)
The problem is that now I have to make multiple "bid" files and compare them with each other and the auction file to determine who won the auction.问题是现在我必须制作多个“出价”文件并将它们相互比较并与拍卖文件进行比较,以确定谁赢得了拍卖。
How can this be accomplished?如何做到这一点?
I'm not sure why you'd do this task with files, as it is very expensive to load files into the program.我不确定您为什么要对文件执行此任务,因为将文件加载到程序中非常昂贵。 However, your task can be accomplished (that is if I accurately understand what you're asking; your question was not written very clearly).但是,您的任务可以完成(也就是说,如果我准确理解了您的要求;您的问题没有写得很清楚)。
First, make sure to include your proper headers from the STL首先,确保包含来自 STL 的正确标头
#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
As you may be able to somewhat tell from the includes and their explanations, here's my plan for the program:正如您可以从包含内容及其解释中了解到的那样,这是我对该计划的计划:
std::filesystem::directory_iterator
, search through all the files in the directory from above and save their contents to an std::vector
使用std::filesystem::directory_iterator
,从上面搜索std::filesystem::directory_iterator
所有文件并将它们的内容保存到std::vector
In practice, that means we may want a vector of structs that have (1) the name of the bidder and (2) their bid.在实践中,这意味着我们可能需要一个结构向量,它具有 (1) 投标人的名称和 (2) 他们的投标。 Then we compare all their bids using a max function (like stated above).然后我们使用最大值函数(如上所述)比较他们的所有出价。 When we find the max, we will be able to very easily find the name bidder that goes with that bid and output to the screen.当我们找到最大值时,我们将能够很容易地找到与该出价相匹配的名称出价者并输出到屏幕上。
The .txt
files (for my solution) have strictly two lines per file. .txt
文件(对于我的解决方案)每个文件有严格的两行。 The first line is the bidder's name, and the second line is the bidder's bid.第一行是投标人的姓名,第二行是投标人的出价。 This will line up exactly with the struct we'll use for loading these files into the program.这将与我们将用于将这些文件加载到程序中的结构完全一致。
So, here's the program (make sure you're using C++17
):所以,这是程序(确保您使用的是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;
}
Please note a few things:请注意以下几点:
C++17
;您需要在C++17
编译它; I used MSVC with Visual Studio 2019我在 Visual Studio 2019 中使用了 MSVC#include
s that weren't needed to explain our gameplan, but I added for formatting or making my life easier or something还有其他#include
不需要解释我们的游戏计划,但我添加了格式或让我的生活更轻松或其他什么// Some debugging feedback
if you want如果需要,您可以删除“ // Some debugging feedback
”下的部分Additionally, I generated a random set of data with this Batch file:另外,我用这个批处理文件生成了一组随机数据:
@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
After running that Batch file and then running my program, here was my output:运行该批处理文件然后运行我的程序后,这是我的输出:
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!
I hope that helps!我希望这有帮助! If you have any questions, feel free to ask!如果您有任何问题随时问!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.