如何获取给定单词所在的整行并将其存储在变量中?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您没有说明如何读取文件,而是 fgets 函数 会做你想做的事。
You didn't say how you're reading the file, but the fgets function will do what you want.
使用
ifstream
、getline
和unordered_map
:现在你可以做
Use
ifstream
,getline
andunordered_map
:Now you can do
您可以创建一个结构,并通过重载流提取运算符来读取该结构的数据:
编辑 1:
用法:
就存储而言,查找
std::map
结构并复制 ID 字段并将其用作键。我仍然建议这项工作更适合数据库或电子表格。
You could create a structure and have the structure read in its data by overloading the stream extraction operator:
Edit 1:
Usage:
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.
要一次读取一行文件,您可以使用
标头中定义的std::getline
函数(我假设您正在使用还有fstream
库):不过,函数
std::getline
的优点在于它允许使用更简洁的语法: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 thefstream
library as well):What's nice about the function
std::getline
, though, is that it allows for this much cleaner syntax:谢谢大家的回答,我终于弄清楚了,我使用了这个功能:
选项变量是 switch 语句的一部分,询问您是否要删除或修改记录,2 表示修改,3 表示删除
thank all of you for your answers, I finally figured it out , I used this function :
option variable is part of a switch statement which asks if you want to delete or modify the record, 2 means modify , 3 delete