如何使用CIN从文件中读取值(64位整数)

时间:2011-02-20 17:26:14

标签: c++ stl

假设我的文件input.txt包含以下数字:

2 1 1 888450282
1 2

我需要在单独的变量(a,b,c,d)中读取第一行。 大值可以像64位整数一样大。如何使用C ++ IO执行此操作? 第二行可以有1到N个值。

通常我在C中这样做但是我想学习C ++库,而且我对C ++中的64位整数也不太舒服。

2 个答案:

答案 0 :(得分:1)

您以通常的方式使用iostream,即将其读取为64位大小的整数:

 #include <stdint.h>

 uint64_t value;
 std::cin >> value;
顺便说一句,您也可以使用

形式的stdio
 #include <inttypes.h>
 #include <stdint.h>

 uint64_t value;
 fscanf(file, "%"PRiu64"", &value);

答案 1 :(得分:1)

如果您只关心文件的第一行,可以使用以下内容来获取它。

#include <fstream>
#include <iostream>

以下代码将处理该文件。

    ifstream file("yourfile.txt", ios::in);
    int a, b, c;
    long long d;
    file >> a >> b >> c >> d;
    printf("a: %d, b: %d, c: %d, d: %lld", a, b, c, d);

    file.close();