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

时间:2011-09-02 17:33:58

标签: c++ find line record

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

my TXt file has this structure :
clients.txt
085958485 Roland Spellman rolandl@gmail.com 
090545874 KATHLEEN spellman kathleen1@hotmail.com 
056688741 Gabrielle Solis desperate@aol.com 

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

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

变量= 090545874 KATHLEEN spellman kathleen1@hotmail.com 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所在的整行 因此,如果用户输入090545874,我需要在文本文件中找到它,但我需要在这种情况下得到全线090545874 KATHLEEN spellman kathleen1@hotmail.com 所以我需要将它存储到一个新变量中     string newvariable;     newvariable = 090545874 KATHLEEN spellman kathleen1@hotmail.com

5 个答案:

答案 0 :(得分:2)

要一次读取一行文件,您可以使用std::getline标题中定义的<string>函数(我假设您正在使用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;
}

答案 1 :(得分:1)

谢谢大家的答案,我终于明白了,我使用了这个功能:

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表示删除

答案 2 :(得分:0)

你没有说你是如何阅读文件的,但是fgets function会做你想要的。

答案 3 :(得分:0)

使用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;
}

答案 4 :(得分:0)

您可以通过重载流提取运算符来创建结构并在其数据中读取结构:

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字段并将其用作密钥。

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