查找并提取文件中的某个值

Find and extract a certain value in a file

本文关键字:提取 文件 查找      更新时间:2023-10-16

做一个需要加载现有文件的项目,我想知道如何提取某个值,例如:

如果文件Joe Shmoe包含:

Here is your information:
Name:    Joe Shmoe
Address: 123 Street St
Balance: $300

我怎样才能将 Joe Shmoe、123 Street St 和 300 提取到单个变量中?(不知道为什么有些词是蓝色的,它们只是应该是正常的(

std::getlineignore的小解释,in总是一个输入流。

std::string s;
char c = 'n';
std::getline(in, s, c);

读取当前行的其余部分(因此最多c个(。读取部分从in中删除,除c之外的所有内容都放入s。如果不提供c,将使用默认'n'(行尾(。

std::streamsize len = 1; 
int c = -1; // You can also take a char and write for example char c = 'n' to ignore to the end of the line.
in.ignore(len, c);

将从in中删除最多len个字符。如果它到达c,它就会停止(-1说它永远不会在这条路上停下来(。默认值如上,但通常使用len = std::numeric_limits<std::streamsize>::max()(输入参数可以具有的最大值(和char c = 'n'。然后它基本上会做std::getline(in, s)会做的事情,但丢弃字符串(它实际上永远不会创建它(。