从 txt 文件中读取由字符分隔的元素
Read elements from a txt file that are separeted by a character
我正在开发一个程序,文件中有名字、姓氏和数字,我需要将这些信息读入我的程序。我的实际问题是有些人没有第二个名字或第二个姓氏。为了解决这个问题,我开始尝试从文件中读取直到找到特定字符,例如:
Robert, Ford Black,208 //Where Robert is the first name, and Ford Black are his two last names
George Richard, Bradford,508 //Where George Richard are both his first names, and Bradford is his only last
name
我将此信息保存在三个分隔的字符串中,一个将存储名字和第二个名字,第一个姓氏和第二个姓氏,第三个用于数字。
我正在尝试仅使用来自 C++ 的本机库。
我一直在阅读 getline(a,b,c) 和 IStringStream 实际上可以解决我的问题,但我不知道如何正确实现它
只是用std::getline
加上分隔符来读出字符串流而已。请参阅下面的简化示例(无错误检查):
for (std::string line; std::getline(std::cin, line); )
{
std::string firstName, lastName;
std::istringstream iss(line);
std::getline(iss, firstName, ','); // A comma delimits end-of-input
iss >> std::ws; // Skip over any whitespace characters
std::getline(iss, lastName); // Read remaining line
std::cout << "First Name: " << firstName << std::endl;
std::cout << "Last Name: " << lastName << std::endl;
}
请注意 <iomanip>
中使用 std::ws
的行 iss >> std::ws;
是否会吃掉额外的空白字符(在您的示例中出现在逗号之后)。
我假设输入中的 C++ 行注释只是这个问题的注释,而不是实际输入的一部分。
#include<bits/stdc++.h>
using namespace std;
int main()
{
ifstream myfile("files.txt");
string fullname;
while(getline(myfile,fullname,'/')) break; //here im reading till the first / is acquired and the entire string is stored in "fullname"
string firstname,lastname;
size_t pos=fullname.find(',');
firstname=fullname.substr(0,pos); //store the firstname
lastname=fullname.substr(pos+1);// storee the lastname
cout<<firstname<<" "<<lastname;
}
由于提出的问题是读取数字前假设的名称,如果有“/”,您可以读取到第一次出现的 /。这会给你全名。然后在全名上使用 substr 并查找逗号的出现(如果存在)。逗号位置左侧的所有字符将构成您的名字,逗号位置右侧的其余字符将构成您的姓氏。
我正在开发一个程序,文件中有名字、姓氏和数字,我需要将这些信息读入我的程序。我的实际问题是有些人没有第二个名字或第二个姓氏。为了解决这个问题,我开始尝试从文件中读取直到找到特定字符,例如:
Robert, Ford Black,208 //Where Robert is the first name, and Ford Black are his two last names
George Richard, Bradford,508 //Where George Richard are both his first names, and Bradford is his only last
name
我将此信息保存在三个分隔的字符串中,一个将存储名字和第二个名字,第一个姓氏和第二个姓氏,第三个用于数字。
我正在尝试仅使用来自 C++ 的本机库。 我一直在阅读 getline(a,b,c) 和 IStringStream 实际上可以解决我的问题,但我不知道如何正确实现它
只是用std::getline
加上分隔符来读出字符串流而已。请参阅下面的简化示例(无错误检查):
for (std::string line; std::getline(std::cin, line); )
{
std::string firstName, lastName;
std::istringstream iss(line);
std::getline(iss, firstName, ','); // A comma delimits end-of-input
iss >> std::ws; // Skip over any whitespace characters
std::getline(iss, lastName); // Read remaining line
std::cout << "First Name: " << firstName << std::endl;
std::cout << "Last Name: " << lastName << std::endl;
}
请注意 <iomanip>
中使用 std::ws
的行 iss >> std::ws;
是否会吃掉额外的空白字符(在您的示例中出现在逗号之后)。
我假设输入中的 C++ 行注释只是这个问题的注释,而不是实际输入的一部分。
#include<bits/stdc++.h>
using namespace std;
int main()
{
ifstream myfile("files.txt");
string fullname;
while(getline(myfile,fullname,'/')) break; //here im reading till the first / is acquired and the entire string is stored in "fullname"
string firstname,lastname;
size_t pos=fullname.find(',');
firstname=fullname.substr(0,pos); //store the firstname
lastname=fullname.substr(pos+1);// storee the lastname
cout<<firstname<<" "<<lastname;
}
由于提出的问题是读取数字前假设的名称,如果有“/”,您可以读取到第一次出现的 /。这会给你全名。然后在全名上使用 substr 并查找逗号的出现(如果存在)。逗号位置左侧的所有字符将构成您的名字,逗号位置右侧的其余字符将构成您的姓氏。