c++ - 如何仅将整数从文件读取到也包含字符串的c++程序中?

标签 c++ file-io

我有一个包含数字和整数的文件,我只想读取整数,
如果它们令人讨厌,请忽略宏,但是我只需要有整数,但是我必须确保还要读取字符串,然后忽略它们
我必须在这里修改什么:

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define mod 1000000007
#define mp(a,b) make_pair(a,b)
typedef long double ld;
#define getl(s) getline(cin,s)
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

  
  
  
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;
}

示例文件可以是:
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

最佳答案

您可以将文本文件中的值读入字符串 vector 。在这里,您可以使用标准库及其算法从字符串中提取整数。在此特定解决方案中,我选择仅使用<regex>库来协助字符的模式匹配。 <regex>的使用实际上不会从字符串中提取值,也不会对其进行转换,它只是检查数字是否存在...这不适用于每种用例...例如,如果字符串包含:"abc123def456",此特定实现将返回123456的整数值。您可以修改它以满足您的特定需求。您可以搜索正则表达式,以查看如何在给定字符串中进行模式匹配以及查找特定字符或字符序列。这仅是为了说明如何使用该库来提供这种功能。

#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;
}
使用您提供的示例数据集,我可以将结果以及垃圾字符串的结果打印到控制台中。
520 250 120 500 60 45 700 35 300 100
123456
还有其他方法可以解决此问题,许多人会选择将<sstream>std::stringstreamstd::istringstream一起使用,因为它们可以执行类似的操作而无需使用正则表达式。

关于c++ - 如何仅将整数从文件读取到也包含字符串的c++程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64618204/

相关文章:

c - 在 Ubuntu LInux 中使用 C API 锁定和解锁文件

c++ - 将 std::ifstream 读入线 vector

c - 删除C中特定文件夹中的文件

c++ - 翻译 Assimp 3D 模型也旋转

c++ - 如何从 C++ 中的文本文件中删除一个空行?

c++ - 如何使用 Int 65H 输出字符串?

php 文件在运行时重命名 - 是否可以锁定?

c++ - 在 C++ 中从多维 vector 创建和获取值的问题

c++ - 如何设计一个始终是另一个类的成员的类

c - 将二进制字符串作为二进制文件写入文件