如何获取给定单词所在的整行并将其存储在变量中?

发布于 2024-12-02 20:07:23 字数 2363 浏览 0 评论 0原文

我是 C++ 世界的初学者,我需要获取给定单词所在的整行并将其存储到变量中。

my TXt file has this structure :
clients.txt
085958485 Roland Spellman [email protected] 
090545874 KATHLEEN spellman [email protected] 
056688741 Gabrielle Solis [email protected] 

因此程序要求用户输入该人的 id,该 id 始终是该行中的第一个数字或单词。 然后用户输入 090545874 该程序必须能够在文本文件中找到 090545874,然后获取将其存储到变量中的整行。

我知道如何在文本文件中查找单词,但我不知道如何将整行放入变量中。所以最后我的变量必须存储

变量 = 090545874 KATHLEENpellman [电子邮件受保护] 4878554

之后,我可以删除整行或记录。 我使用此代码将数据输入txt文件,

      struct person{       

      char id[10];
      char name[20];
      char lastname[20];
      char email[10];
      } clientdata;

      ofstream clientsfile;
      clientsfile.open ("clientes.dat" , ios::out | ios::app);


      if (clientsfile.is_open())
      {
      cout<<"   ENTER THE ID"<<endl;
      cin>>clientdata.id;
      clientsfile<<clientdata.id<<" ";                              

      cout<<"   ENTER THE NAME"<<endl;
      cin>>datoscliente.name;
      clientsfile<<clientdata.name<<" ";

      cout<<"   ENTER THE LAST NAME"<<endl;
      cin>>clientdata.lastname;
      clientsfile<<clientdata.lastname<<" ";

      cout<<"   ENTER THE LAST EMAIL"<<endl;
      cin>>clientdata.email;
      clientsfile<<clientdata.email<<" ";

然后我请求欧盟输入id 我需要做的不仅仅是找到 id,而是获取 id 所在的整行 因此,如果用户输入 090545874 ,我需要在文本文件中找到它,但在这种情况下我需要获取整行 090545874 KATHLEENpellman [电子邮件受保护] 所以我需要将其存储到一个新变量中 字符串新变量; newvariable = 090545874 凯瑟琳斯普曼 [电子邮件受保护]

i am a beginner in the C++ world, i need to get the whole line where a given word is and store it into a variable.

my TXt file has this structure :
clients.txt
085958485 Roland Spellman [email protected] 
090545874 KATHLEEN spellman [email protected] 
056688741 Gabrielle Solis [email protected] 

so the program requests to the user to enter the id of the person, the id is always the first number or word in the line.
the user enters then
090545874
the program has to be able to find the 090545874 in the text file and then get the whole line where it is stored into a variable.

i know how to find a word in a text file but i don't know how to get the whole line into a variable. so at the end my variable has to store

variable = 090545874 KATHLEEN spellman [email protected] 4878554

after that, i am able to delete the entire line or record.
i use this code to enter the data into the txt file

      struct person{       

      char id[10];
      char name[20];
      char lastname[20];
      char email[10];
      } clientdata;

      ofstream clientsfile;
      clientsfile.open ("clientes.dat" , ios::out | ios::app);


      if (clientsfile.is_open())
      {
      cout<<"   ENTER THE ID"<<endl;
      cin>>clientdata.id;
      clientsfile<<clientdata.id<<" ";                              

      cout<<"   ENTER THE NAME"<<endl;
      cin>>datoscliente.name;
      clientsfile<<clientdata.name<<" ";

      cout<<"   ENTER THE LAST NAME"<<endl;
      cin>>clientdata.lastname;
      clientsfile<<clientdata.lastname<<" ";

      cout<<"   ENTER THE LAST EMAIL"<<endl;
      cin>>clientdata.email;
      clientsfile<<clientdata.email<<" ";

then i request to the eu to enter the id
and what i need to do is not to find the id only, it's to get the whole line where the id is
so if the user enters 090545874 , i need to find it in the text file , but i need to get teh whole line in this case 090545874 KATHLEEN spellman [email protected]
so i need to store that into a new variable
string newvariable;
newvariable = 090545874 KATHLEEN spellman [email protected]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天涯沦落人 2024-12-09 20:07:24

您没有说明如何读取文件,而是 fgets 函数 会做你想做的事。

You didn't say how you're reading the file, but the fgets function will do what you want.

最后的乘客 2024-12-09 20:07:24

使用 ifstreamgetlineunordered_map

#include <fstream>
#include <string>
#include <sstream>
#include <exception>
#include <unordered_map>

using namespace std;

ifstream infile("mytextfile.txt");

if (!infile) {
    cerr << "Failure, cannot open file";
    cin.get();
    return 0;
}

unordered_map<string, string> id2line;
string id, line;

while (getline(infile, line)) {
    stringstream strstm(line);
    strstm >> id;
    id2line[id] = line;
}

现在你可以做

cout << "Please enter an id: " << endl;

string id;
cin >> id;

try {
    // unordered_map::at throws an std::out_of_range exception when the key doesn't exist
    string line = id2line.at(id);
    cout << "Info:\n    " << line << endl;
} catch (out_of_range& e) {
    cout << "No information by that id." << endl;
}

Use ifstream, getline and unordered_map:

#include <fstream>
#include <string>
#include <sstream>
#include <exception>
#include <unordered_map>

using namespace std;

ifstream infile("mytextfile.txt");

if (!infile) {
    cerr << "Failure, cannot open file";
    cin.get();
    return 0;
}

unordered_map<string, string> id2line;
string id, line;

while (getline(infile, line)) {
    stringstream strstm(line);
    strstm >> id;
    id2line[id] = line;
}

Now you can do

cout << "Please enter an id: " << endl;

string id;
cin >> id;

try {
    // unordered_map::at throws an std::out_of_range exception when the key doesn't exist
    string line = id2line.at(id);
    cout << "Info:\n    " << line << endl;
} catch (out_of_range& e) {
    cout << "No information by that id." << endl;
}
弥枳 2024-12-09 20:07:24

您可以创建一个结构,并通过重载流提取运算符来读取该结构的数据:

struct Record
{
    unsigned int id;
    std::string first_name;
    std::string last_name;
    std::string email_addr;

    friend std::istream& operator>>(std::istream& input, Record& r);
};

std::istream& operator>>(std::istream& input, Record&r)
{
    input >> r.id;
    input >> r.first_name;
    input >> r.last_name;
    getline(input, r.email_addr);
    return input;
}

编辑 1:
用法:

ifstream input_file("mydata.text");
Record r;
std::map<unsigned int, Record> container;
while (input_file >> r)
{
    unsigned int id = r.id;
    container[id] = r;
}

就存储而言,查找 std::map 结构并复制 ID 字段并将其用作键。

我仍然建议这项工作更适合数据库或电子表格。

You could create a structure and have the structure read in its data by overloading the stream extraction operator:

struct Record
{
    unsigned int id;
    std::string first_name;
    std::string last_name;
    std::string email_addr;

    friend std::istream& operator>>(std::istream& input, Record& r);
};

std::istream& operator>>(std::istream& input, Record&r)
{
    input >> r.id;
    input >> r.first_name;
    input >> r.last_name;
    getline(input, r.email_addr);
    return input;
}

Edit 1:
Usage:

ifstream input_file("mydata.text");
Record r;
std::map<unsigned int, Record> container;
while (input_file >> r)
{
    unsigned int id = r.id;
    container[id] = r;
}

As far as storage is concerned look up the std::map structure and copy the ID field and use it as the key.

I still suggest that this work is a better candidate for a database or spreadsheet.

忱杏 2024-12-09 20:07:23

要一次读取一行文件,您可以使用 标头中定义的 std::getline 函数(我假设您正在使用还有 fstream 库):

#include <fstream>
#include <string>

int main(int argc, char** argv) {
    std::ifstream input_file("file.txt");
    std::string line;

    while (true) {
        std::getline(input_file, line);
        if (input_file.fail())
            break;

        // process line now
    }

    return 0;
}

不过,函数 std::getline 的优点在于它允许使用更简洁的语法:

#include <fstream>
#include <string>

int main(int argc, char** argv) {
    std::ifstream input_file("file.txt");
    std::string line;

    while (std::getline(input_file, line)) {
        // process line now
    }

    return 0;
}

To read files one line at a time, you can use the std::getline function defined in the <string> header (I'm assuming you're using the fstream library as well):

#include <fstream>
#include <string>

int main(int argc, char** argv) {
    std::ifstream input_file("file.txt");
    std::string line;

    while (true) {
        std::getline(input_file, line);
        if (input_file.fail())
            break;

        // process line now
    }

    return 0;
}

What's nice about the function std::getline, though, is that it allows for this much cleaner syntax:

#include <fstream>
#include <string>

int main(int argc, char** argv) {
    std::ifstream input_file("file.txt");
    std::string line;

    while (std::getline(input_file, line)) {
        // process line now
    }

    return 0;
}
不气馁 2024-12-09 20:07:23

谢谢大家的回答,我终于弄清楚了,我使用了这个功能:

bool mostshow(int option, string id)
{
 if (option== 2 && id== id1)
 return true;

 if (option== 3 && id== id1)
 return true;

 return false;

}

and this other one

void showline(string field1, string field2, string field3, string field4, string field5, string field6,    string field7)
{   
string store;
store = field1+" "+field2+" "+field3+" "+field4+" "+field5+" "+field6+" "+field7;

cout<<endl;
}

and then in the main 

ifstream myfile("clients.dat", ios::in);    
                  if (!myfile)
                     {
                     cerr <<"   CANNOT OPEN THE FILE!!"<<endl;
                     exit(1); 
                     }

                  option=2;

                  myfile >> field1 >> field2 >> field3 >> field4 >>field5 >> field6 >> field7;



                  while (!myfile.eof())
                  {
                        if (mostshow(option, id))
                           {
                            showline(field1, field2, field3, field4, field5, field6, field7);
                           }


                  myfile >> field1 >> field1 >> field1 >> field1 >>field1 >> field1 >> field1;

                  } 

                  myfile.close();  

选项变量是 switch 语句的一部分,询问您是否要删除或修改记录,2 表示修改,3 表示删除

thank all of you for your answers, I finally figured it out , I used this function :

bool mostshow(int option, string id)
{
 if (option== 2 && id== id1)
 return true;

 if (option== 3 && id== id1)
 return true;

 return false;

}

and this other one

void showline(string field1, string field2, string field3, string field4, string field5, string field6,    string field7)
{   
string store;
store = field1+" "+field2+" "+field3+" "+field4+" "+field5+" "+field6+" "+field7;

cout<<endl;
}

and then in the main 

ifstream myfile("clients.dat", ios::in);    
                  if (!myfile)
                     {
                     cerr <<"   CANNOT OPEN THE FILE!!"<<endl;
                     exit(1); 
                     }

                  option=2;

                  myfile >> field1 >> field2 >> field3 >> field4 >>field5 >> field6 >> field7;



                  while (!myfile.eof())
                  {
                        if (mostshow(option, id))
                           {
                            showline(field1, field2, field3, field4, field5, field6, field7);
                           }


                  myfile >> field1 >> field1 >> field1 >> field1 >>field1 >> field1 >> field1;

                  } 

                  myfile.close();  

option variable is part of a switch statement which asks if you want to delete or modify the record, 2 means modify , 3 delete

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文