String 如何在字符串中查找字符串? 只是我偶尔要用C++写一次。我的问题是如何在字符串中找到字符串?然后保存该行,然后在该行中查找一组数字。例如,我有一个如下所示的文本文件
地址长度名称 87623498 2只狗 12345678 4类 98737289 1只鸟 我想搜索cat,然后将与之相关的数字12345678和4存储到不同的变量名中。下面是我写的一些代码,但还不太正确。我有一个调用此DLL的.exe。感谢您的帮助! ` 包括 包括 包括 包括 包括String 如何在字符串中查找字符串? 只是我偶尔要用C++写一次。我的问题是如何在字符串中找到字符串?然后保存该行,然后在该行中查找一组数字。例如,我有一个如下所示的文本文件,string,char,cstring,getline,String,Char,Cstring,Getline,地址长度名称 87623498 2只狗 12345678 4类 98737289 1只鸟 我想搜索cat,然后将与之相关的数字12345678和4存储到不同的变量名中。下面是我写的一些代码,但还不太正确。我有一个调用此DLL的.exe。感谢您的帮助! ` 包括 包括 包括 包括 包括 char* File_Path; //Path is sent by another program char* Name; //value is being sent by another program
char* File_Path; //Path is sent by another program
char* Name; //value is being sent by another program
unsigned long Address;
int Length;
int found = 0;
{
using namespace std;
std::string Line;
std::ifstream infile;
infile.open (File_Path);
if (infile.is_open())
{
std::ofstream outfile;
outfile.open ("C:\\debug\\test.txt")
while (infile.good() && found == 0)
{
std::getline(infile,Line);
//if (Name is within Line){
/*I want to say IF char* Name = a part of the string line
ie. Name = cat and the string Line is (12345678 4 cat)
Then store the number (12345678 = Address), and
(4 = Length) I hope that makes sense :/
*/
outfile << Line; //output the stored line for debug
outfile << address; //output the stored address for debug
outfile << length; //output the stored length for debug
found = 1;
}
outfile.close();
infile.close();
}
return 0;
}
`
请帮忙!谢谢大家!家庭作业,伪装成一个怪人,我忘记了如何做这种事情,我只是用这种语言工作。实际上,更多的是一个电力电子工程师,就像我说的,从来没有写过C++。实际上是为了工作,但我会弄明白的。谢谢约翰尼的帖子,谢谢。