简体   繁体   中英

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

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?

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;

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM