只是偶尔我必须用 C++ 编写。我的问题是如何在字符串中找到字符串?然后保存该行,然后在该行中查找一组数字。例如,我有一个看起来像这样的文本文件。
地址 长度 名称 87623498 2 狗 12345678 4 猫 98737289 1 鸟
我想搜索“猫”,然后将与其关联的数字(12345678)和(4)存储到不同的变量名中。这是我编写的一些代码,但它并不接近正确。我有一个调用这个 DLL 的 .exe。任何帮助表示赞赏!` #include #include #include #include #include
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;
}
`请帮忙!谢谢!