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


I'm starting in C ++ world, I have to get the full line where there are given words and it's a variable

  This structure is in my TXT file: clients.txt 085958485 Roland Spellman rolandl@gmail.com 090545874 Kathleen Spellman Cathleen 1@hotmail.com 056688741 Gabriele Solis desperate@aol.com   

The program request to enter the user's ID, the ID is always the first number or word in the line. The user then enters 090545874, the program should be able to find 090545874 in the text file and then get the full line where it is stored in a variable.

I know how to find a word in a text file, but I do not know how to go the whole line in a variable, so to store my variable at the end

Variable = 090545874 Kathleen Spellman Kathleen 1@hotmail.com 4878554

After that, I'm able to delete the entire line or record. I use this code to enter data in the text file

  struct person {char id [10]; Four names [20]; Four last names [20]; Four emails [10]; } Client data; Offream clientfile; Clientsfile.open ("clientes.dat", IOS :: out | iOS :: app); If (clientsfile.is_open ()) {cout & lt; & Lt; "Enter ID" & lt; & Lt; Endl; CIN & gt; & Gt; Clientdata.id; Clientsfile & lt; & Lt; Clientdata.id & lt; & Lt; ""; Cout & lt; & Lt; "Enter name" & lt; & Lt; Endl; CIN & gt; & Gt; Datoscliente.name; Clientsfile & lt; & Lt; Clientdata.name & lt; & Lt; ""; Cout & lt; & Lt; "Enter last name" & lt; & Lt; Endl; CIN & gt; & Gt; Clientdata.lastname; Clientsfile & lt; & Lt; Clientdata.lastname & lt; & Lt; ""; Cout & lt; & Lt; "Enter last email" & lt; & Lt; Endl; CIN & gt; & Gt; Clientdata.email; Client file & lt; & Lt; Clientdata.email & lt; & Lt; "";   

Then I request the EU to enter the ID and what I need to do is not find the ID only, it is to get the full line where the user has to enter 090545874, I need to find it in the text file, but I need to get the complete line in this case 090545874 Kathleen Spellman Catholine 1@hotmail.com, so I stored it in a new variable string new form. Sector is required; New semen = 090545874 Kathleen Spellman Cathleen 1@hotmail.com

To read one line at a time , You can & lt; String & gt; Use the std :: getline function defined in the header <(I assume that you are also using fstream library):

  #include & lt; Fstream & gt; #include & lt; String & gt; 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 ()) breaks; // process line now} Return 0; }   

What's good about the std :: getline function, however, it allows for very clean syntax:

  #include & lt; Fstream & gt; #include & lt; String & gt; Int main (int argc, char ** argv) {std :: ifstream input_file ("file.txt"); Std :: string line; While (std :: getline (input_file, line)) {// process line now} 0 returned; }    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -