正确提取到载体
Extract properly to a vector
本文关键字:提取 更新时间:2023-10-16
我制作了一个程序,可以从文本文件中读取,将组件添加到结构中,并将每个结构元素添加到向量中。问题是,由于某种原因,阅读功能似乎根本不起作用。
这是代码:
struct address
{
string street;
int zip;
string city;
};
struct person
{
string name;
string id;
address location;
};
istream & operator >>(istream & in, person & p)
{
getline(in, p.name);
getline(in, p.id);
getline(in, p.location.street);
return in;
}
void read_file(string filename)
{
vector<person> persons;
ifstream input;
input.open(filename);
if (!input.is_open())
{
cout << "File Not Found" << endl;
}
person temp;
input >> temp;
persons.push_back(temp);
input.close();
if (persons.size() == 0)
{
cout << "The are no contacts" << endl;
}
else cout << "There are" << " " << persons.size() << " " << "contacts in the file" << endl;
}
文本文件中的行如下所示:
Test Name Surname
Id 1234567890
Adress, postcode, city
当我说它不起作用时,我的意思是读取函数不显示向量中是否有元素。
这是文本文件:名称.txt
主代码:
int _tmain(int argc, _TCHAR* argv[])
{
const string filename = "C://Users/TEMP/Desktop/lab1/names.txt";
int userinput = 0;
while (true)
{
cout << "Menu: n1) Sök del av personnamnn2) Sök stadern3) Exit" << endl;
cin >> userinput;
cout << endl;
if (userinput == 1)
{
namnsok;
}
else if (userinput == 2)
{
stadsok;
}
else if (userinput == 3)
{
break;
}
}
return 0;
}
我不得不改变行 input.open(filename);
input.open(filename.c_str(),ifstream::in);
让它为我工作。不过,您只会从输入中读取第一人称。
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- C++从通用引用中提取实际类型
- 从运行服务的应用程序代码中提取窗口句柄
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- std::unordered_map::提取引用/指针失效