[英]How do I read only integers from a file into a c++ program that also has strings?
I have a file that contains, numbers(ints) and words both, I want to read only the integers, Please ignore the macros if they are annoying, However I only need to have the integers but I have to be sure to also read the strings and then disregard them What do I have to modify here:我有一个包含数字(整数)和单词的文件,我只想读取整数,如果宏很烦人,请忽略它们,但是我只需要整数,但我必须确保也阅读字符串,然后忽略它们 我必须在这里修改什么:
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
int main(){
FastIO;
ifstream fin("input.txt",ios::in);
int minsal =10000000;
int num;
cout<<minsal;
fin.close();
ofstream fout("output.txt",ios::out);
fout.close();
return 0;
}
An example file could be :一个示例文件可能是:
Vineel Phatak, 520, Amkart, NOBODY
Ajay Joshi, 250, Amkart, Vineel Phatak
Abhishek Chauhan, 120, Amkart, Ajay Joshi
Jayesh Godse, 500, Airflix, NOBODY
Vijaya Mundada, 60, Amkart, Abhishek Chauhan
Shital Tuteja, 45, Airflix, Jayesh Godse
Rajan Gawli, 700, Amkart, Vineel Phatak
Sheila Rodrigues, 35, Amkart, Vineel Phatak
Zeba Khan, 300, Airflix, Jayesh Godse
Chaitali Sood, 100, Airflix, Zeba Khan
you need to fetch your input file line by line and then in each line get the number from it , this is an example to how get one or more number from line using regex :您需要逐行获取输入文件,然后在每一行中从中获取数字,这是如何使用正则表达式从行中获取一个或多个数字的示例:
#include <iostream>
#include <regex>
using namespace std;
int main() {
int x;
string s = "aaa 123 zefdz";
smatch sm;
regex r = regex("\\d+");
regex_search(s, sm, r);
for (string s : sm) {
x = atoi(s.c_str());
cout << "x : " << x;
}
}
in this case result will be :在这种情况下,结果将是:
x : 123
You can read the values from the text file into a vector of strings.您可以将文本文件中的值读取到字符串向量中。 From there you can use the standard library and its algorithms to extract integers from strings.从那里您可以使用标准库及其算法从字符串中提取整数。 In this particular solution, I choose to use the <regex>
library only to assist in pattern matching of characters.在这个特定的解决方案中,我选择仅使用<regex>
库来协助字符的模式匹配。 The use of <regex>
does not actually extract the values out of the string nor does it convert it, it simply checks to see if a digit exists... This will not work for every use case... for example, if a string contains: "abc123def456"
, this particular implementation will return an integer value of 123456
. <regex>
的使用实际上并不从字符串中提取值,也不对其进行转换,它只是检查数字是否存在......这不适用于每个用例......例如,如果字符串包含: "abc123def456"
,这个特定的实现将返回一个整数值123456
。 You can modify this to fit your particular needs.您可以修改它以满足您的特定需求。 You can do searches for regular expressions to see how you can pattern match and look for specific characters or sequences of characters within a given string.您可以搜索正则表达式,以了解如何进行模式匹配以及如何在给定字符串中查找特定字符或字符序列。 This is only to illustrate how one can use the library to provide such functionality.这只是为了说明如何使用该库来提供此类功能。
#include <fstream>
#include <iostream>
#include <optional>
#include <regex>
// #include <sstream> // not used in this implementation
#include <string>
#include <vector>
std::vector<std::string> readFromFile(const char* filename) {
std::fstream file(filename, std::ios::in);
std::vector<string> contents;
std::string line;
if ( file.is_open() ) {
while ( getline(file, line) )
contents.push_back(line);
file.close();
}
return contents;
}
static const std::regex digits("([0-9])");
bool matches(const std::string& s, const std::regex& regex ) {
std::smatch base_match;
return std::regex_match(s, base_match, regex);
}
int parseString( const std::string& data ) {
std::string temp;
for (auto& c : data) {
if (matches(std::string{c}, digits))
temp.push_back(c);
}
return std::stoi(temp);
}
int main() {
std::vector<std::string> fileContents = readFromFile("sample.txt");
std::vector<int> extractedData;
for (auto& s : fileContents)
extractedData.push_back(parseString(s));
for (auto& i : extractedData)
std::cout << i << ' ';
std::cout << '\n';
// This is to demonstrate that care needs to be taken!!!
const std::string garbage{ "Hello 123 World456!" };
auto res = parseString(garbage);
std::cout << res << '\n';
return 0;
}
With the example data set that you provided, I was able to print this to the console as a result as well as the result from the garbage string...使用您提供的示例数据集,我能够将其作为结果以及垃圾字符串的结果打印到控制台...
520 250 120 500 60 45 700 35 300 100
123456
There are other ways to solve and tackle this problem, many will choose to use <sstream>
with either std::stringstream
or std::istringstream
as they can perform similar operations without using regular expressions.还有其他方法可以解决和解决这个问题,许多人会选择将<sstream>
与std::stringstream
或std::istringstream
因为它们可以在不使用正则表达式的情况下执行类似的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.