gpt4 book ai didi

c++ - 查找整数之间的字符

转载 作者:行者123 更新时间:2023-11-28 03:51:39 25 4
gpt4 key购买 nike

我想用 C++ 编写一个程序,它从一个包含信息的文件中获取输入像这样:

physic 17
math 20
programming 10

如果我想在这个文件中给出整数,而不想要像 physic 等字符我应该包含什么库以及我应该使用什么函数

最佳答案

当您使用像 fstream 这样的文件流时并读取字符串,你从流中得到一个词。所以尝试创建 fstream s 和:

fstream s("file.txt",ios::in);
string word;
int number;
s >> word;
s >> number;
// do something with 17 (you got it in number variable)
s >> word;
s >> number;
// do something with 20 (you got it in number variable)
//and so on...

关于c++ - 查找整数之间的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5281789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com