SystemC with c++ - how to print a sc_bigint variable?

1.4k views Asked by At

I have a variable declared as so: sc_bigint<88> x

I want to use fprintf to print it to a file, but this produces an error. I am able to print the variable using cout, but I need to print it to a specific file I have opened.

Any ideas how to do this? Maybe a simple way to redirect cout to the file I need?

2

There are 2 answers

0
Puneet Goel On

Try the File I/O streams provided by C++.

#include <fstream>
#include <iostream>
using namespace std;

// .. snip

// open a file in write mode.
ofstream outfile;
outfile.open("afile.dat");

sc_bigint<88> x;
outfile << x;
0
DarrylLawson On

Using C++'s stream-based IO (as shown in the other answer) is probably the best approach, however, if you really want to use fprintf(), then you have the option of using the sc_dt::sc_bigint<W>::to_string() method. For example:

#include <systemc>
#include <cstdio>

using namespace std;

int sc_main(int argc, char **argv) {
    FILE *fp = fopen("sc_bigint.txt", "w");

    sc_dt::sc_bigint<88> x("0x7fffffffffffffffffffff");  // (2 ** 87) - 1
    fprintf(fp, "x = %s (decimal)\n", x.to_string().c_str());
    fprintf(fp, "x = %s (hexadecimal)\n", x.to_string(sc_dt::SC_HEX).c_str());

    return EXIT_SUCCESS;
}

The above SystemC program writes the following to a file sc_bigint.txt:

x = 154742504910672534362390527 (decimal)
x = 0x7fffffffffffffffffffff (hexadecimal)