如何从如下所示的文件中读取特定数据

How to read specific data from a file that looks like this?

本文关键字:读取 数据 文件      更新时间:2023-10-16

我想做一个程序,通过类的输入函数在txt文件中输入参与者的数据。然后,输出函数用于通过键入单个参与者的ID一次提取其信息。

在我的这段代码中,只要我输入 ID,我的 while 循环就会无限运行。我怀疑它无法找到 eof((。任何帮助将不胜感激。我是C++新手。

#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
class Participant{
private:
    int id, score;
    string name;
public:
    Participant(){
        id = 0; score = 0; name = "";
    }
    void input(){
        char choice;
        ofstream in;
        in.open("Participant.txt", ios::app);
        do{
            cout<<"Enter your ID:   t";
            cin>>id;
            cout<<"Enter your name: t";
            cin>>name;
            cout<<"Enter your Score:t";
            cin>>score;
            in<<name<<" ";
            in<<id<<" ";
            in<<score<<endl;
            cout<<"Add another entry? (Y/N)n";
            cin>>choice;
        }while(choice == 'y' || choice == 'Y');
        in.close();
    }
    void output(){
        int idout, holderID, holderS;
        string holder, output;
        cout<<"Enter the ID for more information related to the person:"; 
        cin>>idout;
        fstream out;
        out.open("Participant.txt");
        while(!out.eof()){
            out>>holderID;
            cout<<"looping...n";
            if(idout == holderID){
                out>>holder;
                cout<<"Name: t"<<holder<<endl;
                out>>holderS;
                cout<<"Score:t"<<holderS<<endl;
                holder ="";
                holderS=0;
                break;
            }
            else continue;
        }
        out.close();
    }
    void max(){
    }
};
int main(){
char choice;
Participant player;
cout<<"Asking for Input: n";
player.input();

system("pause");
system("cls");
cout<<"Data Viewing: n";
do{
    player.output();
    cout<<"nDo you wish to extract information on other players?n";
    cout<<"Y - Yes."<<endl;
    cout<<"N - No."<<endl;
    cout<<"Choice: ";
    cin>>choice;
}while (choice == 'y' || choice == 'Y');
cout<<"nnEnd of Data Viewing.n";
}

我希望它首先只读取 ID,在第一行是 1037。如果 ID 匹配,它应该显示文件中接下来的 2 个成员;名称和分数。

问题是您尝试直接从外流使用 holderID (int(。尝试使用字符串读取相同的 out 值,并使用 stoi(( 将其转换为 int。另请注意,当您编写第一个是名称,后跟 id 和分数。

另请使用以下内容作为参考。我使用 std::map 来存储 id、name 和 score 的值。

#include <string>
#include <fstream>
#include <map>
#include <iostream>
#include <algorithm>
#include <sstream>
class Participants
{
    int id;
    int score;
    std::string name;
public:
    Participants(): id(0), score(0)
    {}
    Participants(int id, int score, std::string name): id(id), score(score), name(name)
    {}
    ~Participants()
    {}
    int GetId()
    {
        return id;
    }
    std::string encode()
    {
        auto strRet = std::string( name + " " + std::to_string(id) + " " + std::to_string(score) + "n");
        return  strRet;
    }
    void decode(std::string text)
    {
        std::stringstream ss(text);
        std::string buf;
        //Read Name
        std::getline( ss, buf , ' ');
        name = buf;
        //Read id
        std::getline( ss, buf , ' ');
        id = std::stoi( buf );
        //Read Score
        std::getline( ss, buf , 'n');
        score = std::stoi( buf );
    }
};
class DataReader
{
    std::string fileName;
    std::fstream myfile;
public:
    DataReader(std::string fileName): fileName(fileName)
    {
    }
    ~DataReader()
    {
    }
    void ReadParticipants(std::map<int, Participants> &MapParticipants)
    {
        myfile.open(fileName, std::ios::in);
        MapParticipants.clear();
        if ( myfile.is_open() )
        {
            std::string line;
            while ( std::getline(myfile, line) )
            {
                Participants oParticipants;
                //Decode and Add to map
                oParticipants.decode(line);
                //Add to map
                MapParticipants[ oParticipants.GetId() ] = oParticipants;
            }
        }
        myfile.close();
    }
    void WriteParticipants(std::map<int, Participants> &MapParticipants)
    {
        //Load Map to find Duplicates
        std::map<int, Participants> MapParticipants_exist;
        ReadParticipants(MapParticipants_exist);
        myfile.open(fileName, std::ios::app);
        if ( myfile.is_open() )
        {
            for ( auto oParticipants : MapParticipants)
            {
                //Check for Duplicates (to Write or not)
                if ( MapParticipants_exist.find(oParticipants.first) ==  MapParticipants_exist.end() )
                {
                    auto text = oParticipants.second.encode();
                    myfile << text.c_str();
                }
            }
        }
        myfile.close();
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    DataReader oReader("File.txt");
    std::map<int, Participants> MapParticipants;
    //Make Some Participants
    Participants p1(1, 50, "TOM");
    Participants p2(2, 40, "TIM");
    Participants p3(3, 80, "JERRY");
    //Add them to map
    MapParticipants[p1.GetId()] = p1;
    MapParticipants[p2.GetId()] = p2;
    MapParticipants[p3.GetId()] = p3;

    oReader.WriteParticipants(MapParticipants);
    oReader.ReadParticipants(MapParticipants);
    //Find and Display
    int id = 2;
    auto it = MapParticipants.find(id);
    if ( it != MapParticipants.end() )
    {
        //Show/Print
        ...
    }
    return 0;
}