简体   繁体   English

使用C ++的SystemC-如何打印sc_bigint变量?

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

I have a variable declared as so: sc_bigint<88> x 我有一个这样声明的变量:sc_bigint <88> x

I want to use fprintf to print it to a file, but this produces an error. 我想使用fprintf将其打印到文件中,但这会产生错误。 I am able to print the variable using cout, but I need to print it to a specific file I have opened. 我可以使用cout打印变量,但是我需要将其打印到我打开的特定文件中。

Any ideas how to do this? 任何想法如何做到这一点? Maybe a simple way to redirect cout to the file I need? 也许将cout重定向到我需要的文件的简单方法?

Try the File I/O streams provided by C++. 尝试使用C ++提供的文件I / O流。

#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. 使用C ++基于流的IO(如其他答案所示)可能是最好的方法,但是,如果您确实想使用fprintf() ,则可以选择使用sc_dt::sc_bigint<W>::to_string()方法。 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 : 上面的SystemC程序将以下内容写入文件sc_bigint.txt

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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