【问题标题】:SystemC sc_uint from String ObjectSystemC sc_uint 来自字符串对象
【发布时间】:2018-05-22 22:25:04
【问题描述】:

我最近开始使用SystemC,想编写一个简单的程序,从SystemC 字符串格式的文件中读取数字并将它们转换为sc_uint 类型。不知何故,这个简单的程序在转换过程中总是失败

程序:

#include <systemc.h>
#include <fstream>
#include <string>

int sc_main( int argc, char *argv[] ){
    ifstream in_file;
    std::string line;
    char *buffer;

    in_file.open( "ex3_2.dat", ios::in );

    while( getline( in_file, line ) ){
        sc_uint<29> x = line.c_str();
    }

    return( 0 );
}

ex3_2.dat 文件

0x1234\n

输出

错误:(E403)转换失败:字符串为空

我不明白为什么转换会中断。

c_str() 应该返回一个 const char*。

将字符串打印到 cout 会显示 line 的正确值。

像“0x1234”这样的静态分配确实有效。 有人有想法吗?

【问题讨论】:

  • 你试过打印字符串吗?是你预期的吗?
  • 是的。打印字符串完全符合我的预期“0x1234”
  • 这个错误还有其他上下文吗?这是运行时异常而不是编译错误,对吧?
  • 没有编译工作没有问题。执行从正常的 SystemC 标头开始,然后是错误。

标签: c++ systemc


【解决方案1】:

以下代码

#include <systemc.h>
#include <fstream>
#include <string>

#include <iostream>

int sc_main( int argc, char *argv[] ){

    std::ifstream in_file;
    std::string line;

    in_file.open( "ex3_2.dat", ios::in );

    while( getline( in_file, line ) ){
        sc_uint<29> x = line.c_str();
       std::cout << "0x" << std::hex << x << '\n';
    }

    return 0;
}

使用 ex3_2.dat 文件

0x1234
5
6
7
8

编译如下

g++ -std=c++11 -Wall -o main.o main.cpp -lsystemc

生产

0x00001234
0x00000005
0x00000006
0x00000007
0x00000008

g++ --version = g++ (GCC) 7.1.0

不会发出编译器警告或错误。结论是代码本身很好。也许在别处寻找错误,链接了哪些标准库版本等。文件中的隐藏字符? ASCII 与 UTF-8?从这里缩小范围的可能性太多了。

【讨论】:

    猜你喜欢
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2015-04-22
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多