gpt4 book ai didi

c++ - 来自字符串对象的 SystemC sc_uint

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:54 25 4
gpt4 key购买 nike

我最近开始使用 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

输出

Error: (E403) conversion failed: character string is empty

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

c_str() should return a const char*.

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

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

最佳答案

下面的代码

#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?从这里缩小范围的可能性太多。

关于c++ - 来自字符串对象的 SystemC sc_uint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50452880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com