开发者

How to get the whole line where a given word is and store it in a variable?

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 vari开发者_Go百科able.

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 

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 kathleen1@hotmail.com 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 kathleen1@hotmail.com so i need to store that into a new variable string newvariable; newvariable = 090545874 KATHLEEN spellman kathleen1@hotmail.com


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


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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜