C ++如何读取txt文件并检索数值,除了字符串(反之亦然)
C++ How to read txt file and retrieve numerical values, apart from string (and vice versa)
我一直在自学 C++,并寻找如何做到这一点。让我举个例子来阐明我的意图。
这里是一个txt文件,内容如下
Matt 18 180.0 88.5
Angela 20 155.5 42.2
每一行都有关于一个人的姓名、年龄、身高和体重的信息。
我一直在尝试做的是分别获取这 4 种类型的信息,并根据信息类型将它们存储在不同的变量中。
vector<string> name; //"Matt" or "Angela" are stored here.
vector<int> age; //18 or 20
vector<double> height; //The same logic goes for this vector and the next one
vector<double> weight;
至少我发现txt文件中的信息可以通过ifstreamObject.open(filename.c_str())
和getline(ifstreamObject, string)
存储在字符串变量中。但是,通过使用这种方法,我只能得到对应于每一行的字符串值。换句话说,我无法区分字符串值和数值。
很可能没有任何其他方法可以从 txt 文件中获取信息。尽管如此,为了以防万一,在我放弃之前,我想在这里询问一些关于如何以这种方式获取信息的建议。
如有任何建议,我们将不胜感激。
如果您知道每个条目之间有一个特定的字符(如制表符),您可以使用 String.find_first_of 和子字符串将字符串分成多个部分并将它们解析为您拥有的字段。
检查 http://www.cplusplus.com/reference/string/string/ 了解更多信息
您好,您可以使用 stl::string 方法来操作行字符串以分别提取这 4 种类型的信息。
使用std::string::find_first_of
to find each start of white space and use std::string::find_first_not_of
to find each non white space characters. use std::string::substr
从行字符串中提取子字符串。还使用 atoi
将字符串值转换为 int。
例如,
//rowSrting holds the data of one line in file
std::size_t nameEnd = rowString.find_first_of(" ");
string name = rowString.substr(0, nameEnd-1);
std::size_t ageFirst = rowString.find_first_not_of(" ", nameEnd);
std::size_t ageEnd = rowString.find_first_of(" ", ageFirst );
int age = atoi(rowString.substr(ageFirst, ageEnd-1));
你可以直接使用流,
std::string name;
int age;
double height, weight;
while(ifstreamObject >> name >> age >> height >> weight)
{
// process name, age, height and weight
}
缺点是流插入运算符将读取到第一个空白。所以,如果你想将整行作为一个字符串来读取,那么使用getline
,相应地处理字符串,"map"将getline
读取的字符串返回到istringstream
,
std::istringstream is(str); // constructs an istringstream from the string str
然后像使用流一样使用 is
。
我一直在自学 C++,并寻找如何做到这一点。让我举个例子来阐明我的意图。
这里是一个txt文件,内容如下
Matt 18 180.0 88.5
Angela 20 155.5 42.2
每一行都有关于一个人的姓名、年龄、身高和体重的信息。
我一直在尝试做的是分别获取这 4 种类型的信息,并根据信息类型将它们存储在不同的变量中。
vector<string> name; //"Matt" or "Angela" are stored here.
vector<int> age; //18 or 20
vector<double> height; //The same logic goes for this vector and the next one
vector<double> weight;
至少我发现txt文件中的信息可以通过ifstreamObject.open(filename.c_str())
和getline(ifstreamObject, string)
存储在字符串变量中。但是,通过使用这种方法,我只能得到对应于每一行的字符串值。换句话说,我无法区分字符串值和数值。
很可能没有任何其他方法可以从 txt 文件中获取信息。尽管如此,为了以防万一,在我放弃之前,我想在这里询问一些关于如何以这种方式获取信息的建议。
如有任何建议,我们将不胜感激。
如果您知道每个条目之间有一个特定的字符(如制表符),您可以使用 String.find_first_of 和子字符串将字符串分成多个部分并将它们解析为您拥有的字段。 检查 http://www.cplusplus.com/reference/string/string/ 了解更多信息
您好,您可以使用 stl::string 方法来操作行字符串以分别提取这 4 种类型的信息。
使用std::string::find_first_of
to find each start of white space and use std::string::find_first_not_of
to find each non white space characters. use std::string::substr
从行字符串中提取子字符串。还使用 atoi
将字符串值转换为 int。
例如,
//rowSrting holds the data of one line in file
std::size_t nameEnd = rowString.find_first_of(" ");
string name = rowString.substr(0, nameEnd-1);
std::size_t ageFirst = rowString.find_first_not_of(" ", nameEnd);
std::size_t ageEnd = rowString.find_first_of(" ", ageFirst );
int age = atoi(rowString.substr(ageFirst, ageEnd-1));
你可以直接使用流,
std::string name;
int age;
double height, weight;
while(ifstreamObject >> name >> age >> height >> weight)
{
// process name, age, height and weight
}
缺点是流插入运算符将读取到第一个空白。所以,如果你想将整行作为一个字符串来读取,那么使用getline
,相应地处理字符串,"map"将getline
读取的字符串返回到istringstream
,
std::istringstream is(str); // constructs an istringstream from the string str
然后像使用流一样使用 is
。