2016-03-07 58 views
1

好的。所以我使用GMP库来计算大数字。我有这样的代码:GMP库 - 文件I/O

#include <stdio.h> 
#include <iostream> 
#include <cstdlib> 
#include <string> 
#include <gmp.h> 


using std::cout; 
using std::endl; 


int main(int argc, char** argv) 
{ 
    FILE *file; 
    file = fopen("data.txt", "wt"); 
    int number=atoi(argv[1]), i=1; 
    mpz_t a; mpz_init(a); 
    mpz_t b; mpz_init(b); 
    mpz_set_ui(b, 1); 
    cout<<a<<endl; 
    for (; number>0; number--, i++) 
    { 
     cout<<i<<". "<<b<<endl; 
     mpz_add(b,b,a); 
     mpz_sub(a,b,a); 
    } 
    mpz_clear(a); 
    mpz_clear(b); 
    fclose(file); 
} 

,我想打印数(A,B)为.txt文件。我该怎么做?试过fprintf(),但它似乎并没有工作

+0

(为什么不使用'gmpxx.h'?)就像你正在使用'cout << a',你可以使用'std :: ofstream f'并执行'f << a' ... –

回答