如何从文件输入和存储长整数以在 C++ 中处理它

How to input and store long ints from a file to process it in a c++

所以我有一个 long int 数据集,在一个 .txt 文件中有大约 3-4 百万个数字,每行一个数字 我想将这些数字作为我程序的输入,我不确定如何正确处理,对于我当前的代码,我遇到了分段错误

long long int *buff;
buff = (long long int*) malloc(100000000 * sizeof(long long int));
FILE *fp;
fp = fopen("out6.txt","r");
while(fscanf(fp,"%lli",buff));
for(int i=0;i!=EOF,i++){
filter(to_string(buff[i]);
}
fclose(fp);
free(buff)

而且我的功能非常简单,所以,如果有一种方法可以避免存储数字,那就太好了

int filter(string num){
   for(int i=0;i<num.size();i++){
       if(num[i]=='0'||num[i]=='8'){
           return 0;
       }
       if((num[i]=='1' && num[i+1]=='8') || (num[i]=='2' && num[i+1]=='9') || (num[i]=='6' && num[i+1]=='3') || (num[i]=='1' && num[i+1]=='2') || (num[i]=='2' && num[i+1]=='1') || (num[i]=='6' && num[i+1]=='1') || (num[i]=='6' && num[i+1]=='1')
        || (num[i]=='9' && num[i+1]=='2') || (num[i]=='3' && num[i+1]=='6') || (num[i]=='1' && num[i+1]=='1') 
        || (num[i]=='8' && num[i+1]=='1') || (num[i]=='4' && num[i+1]=='5') || (num[i]=='5' && num[i+1]=='4') || (num[i]=='1' && num[i+1]=='3') || (num[i]=='1' && num[i+1]=='4') || (num[i]=='4' && num[i+1]=='8') || (num[i]=='8' && num[i+1]=='4') 
        || (num[i]=='7' && num[i+1]=='9')|| (num[i]=='9' && num[i+1]=='7') || (num[i]=='6' && num[i+1]=='6') || (num[i]=='2' && num[i+1]=='4') || (num[i]=='4' && num[i+1]=='2') || (num[i]=='2' && num[i+1]=='7') || (num[i]=='7' && num[i+1]=='2') || (num[i]=='4' && num[i+1]=='7') || (num[i]=='7' && num[i+1]=='4') || (num[i]=='7' && num[i+1]=='1') || (num[i]=='1' && num[i+1]=='7')){
           return 0;
           
           //cout<<"There exists a BAD combination of "<<num[i]<<" and "<<num[i+1]<<endl;
       }
   }
   cout<<num<<endl;
}

编辑#1 所以我添加了过滤功能,我添加了一个for循环来过滤每个数字, 我知道 for 循环是错误的,需要一种方法来遍历数字,而且如果有一种方法可以一次对一个数字使用过滤器而不存储,那也很好。将 %li 更改为 %lli。也可以用字符串输入

我不是 100% 确定你的 objective 是什么,但它看起来像:

  • 您想从文件中读取 long longs。
  • 您想过滤掉其中的一些。
  • 您想通过将数字视为字符串来应用过滤器。

从 C++20 开始,我们有 std::ranges::copy_if 投影:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::ifstream fs("out6.txt");  // open the file

    std::vector<long long> result; // to store all the "ok" numbers

    // copy only those numbers from the file for which the filter returns `true`
    // using a projection to convert each number to a string before the filtering:

    std::ranges::copy_if(
        std::istream_iterator<long long>(fs), // read long long's from "fs"
        std::istream_iterator<long long>{},   // until it fails reading more
        std::back_inserter(result),           // where to store "ok" numbers
        [](const std::string& ll) noexcept {  // filter
            // your complicated filter logic
            // goes in here
            return true; // or false
        },
        [](long long ll) {                    // projection
            return std::to_string(ll);
        });

    // print the result
    for(long long v : result) std::cout << v << '\n';
}

Demo

在 C++11 到 C++17 中,您可以使用 std::copy_if(使用“手动”投影):

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::ifstream fs("out6.txt");  // open the file

    std::vector<long long> result; // to store all the "ok" numbers

    // copy only those numbers from the file for which the filter returns `true`
    // using a projection to convert each number to a string before the filtering:

    std::copy_if(
        std::istream_iterator<long long>(fs), // read long long's from "fs"
        std::istream_iterator<long long>{},   // until it fails reading more
        std::back_inserter(result),           // where to store "ok" numbers
        [](long long in) noexcept {           // filter
            std::string ll = std::to_string(in); // the manual projection part
            // your complicated filter logic
            // goes in here
            return true; // or false
        });

    // print the result
    for(long long v : result) std::cout << v << '\n';
}

Demo