slh*_*hck 2 c++ format int g++ scanf
我想编译一些可以在这里下载的 C++软件.这是从2001年开始.跑步make应该可以解决问题.
我在Ubuntu 10.10上,我不得不gcc在makefile中更改g++以便编译工作.
现在,程序需要扫描参数文件.当我编译时,我收到几个警告:
g++ -c -o packet_data_agent.o packet_data_agent.cpp
packet_data_agent.cpp: In constructor ‘PACKET_DATA_AGENT::PACKET_DATA_AGENT()’:
packet_data_agent.cpp:34: warning: deprecated conversion from string constant to ‘char*’
packet_data_agent.cpp: In member function ‘void PACKET_DATA_AGENT::initialize(FILE*, FILE*)’:
packet_data_agent.cpp:109: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp:128: warning: format ‘%d’ expects type ‘int*’, but argument 3 has type ‘int32*’
packet_data_agent.cpp:131: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp:136: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp:141: warning: format ‘%d’ expects type ‘int*’, but argument 3 has type ‘int32*’
packet_data_agent.cpp:144: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp:149: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp: In member function ‘int32 PACKET_DATA_AGENT::get_PDU(int32, VIRTUAL_PDU*)’:
packet_data_agent.cpp:213: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp:244: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp: In member function ‘void PACKET_DATA_AGENT::put_PDU(int32, VIRTUAL_PDU*)’:
packet_data_agent.cpp:312: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int32’
packet_data_agent.cpp: In member function ‘void PACKET_DATA_AGENT::write_statistics(FILE*)’:
packet_data_agent.cpp:393: warning: format ‘%10d’ expects type ‘int’, but argument 3 has type ‘int32’
packet_data_agent.cpp:394: warning: format ‘%10d’ expects type ‘int’, but argument 3 has type ‘int32’
g++ -O4 mobile_ip.o loss_generator.o radio_link_agent.o packet_data_agent.o -o mobile_ip -lm
Run Code Online (Sandbox Code Playgroud)
这导致我无法解析包含的设置文件.在packet_data_agent.h,变量声明为int32:
int32 compressed_Internet_header_size;
Run Code Online (Sandbox Code Playgroud)
以及用于扫描参数文件的C++代码packet_data_agent.cpp:128:
fscanf(ParameterFile_ptr,"- compressed RTP/UDP/IP header size (in bytes): %d\n",&compressed_Internet_header_size);
Run Code Online (Sandbox Code Playgroud)
但是当我运行程序时,我收到以下错误:
!!error in module PACKET-DATA-AGENT: illegal value 0 for the compressed RTP/UDP/IP header size
Run Code Online (Sandbox Code Playgroud)
虽然参数文件清楚地具有:
- compressed RTP/UDP/IP header size (in bytes): 3
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能使程序正确解析参数文件?
int我平台的大小是4这是我的g ++版本:
$ g++ -v
Using built-in specs.
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
Run Code Online (Sandbox Code Playgroud)
我更改了源代码如下:
int return_test = fscanf(ParameterFile_ptr,"- compressed RTP/UDP/IP header size (in bytes): %d\n",&compressed_Internet_header_size);
printf ("%d\n", return_test);
printf ("%d\n", compressed_Internet_header_size);
Run Code Online (Sandbox Code Playgroud)
输出为0,0.这意味着什么?我的意思是,说真的,它就在我面前,它不会找到参数?
警告:
头global.h使int32的代名词long.当你经过一个gcc被发出警告long成*printf与格式码%d,因为一些平台上(任何一个地方int是32位,一个long是64,或者其中一个int为16位和long32),其将具有非常不好的结果.
你可以改变typedef,这样就int32意味着int而不是long也许什么都不会发生.
运行时的实际错误:
[根据原始提问者的新信息编辑:]
这似乎与int/ long/ int32stuff 没有任何关系:相反,fscanf它无法读取参数文件的那一行.早期的线路怎么样?是否fscanf为它们全部返回1,或者它是否真的出错了,其方式不会让它停止尝试解析文件?
假设所有早期的fscanf调用都成功,这是第一个失败的调用:如果将源代码中的固定样板文件复制并粘贴到文件中会发生什么?如果您只是查找没有任何固定样板文本的数字怎么办?file containing the output RTP stream在行和compressed RTP/UDP/IP header size行之间的参数文件中没有任何其他行,是吗?
您可能能够了解它失败的位置,如下所示:在违规fscanf调用之后,使用读取一些参数文件fread,显示它并退出(当然,没有点试图继续;解析器将完全混淆如果你这样做).例如,如果它因为遇到与fscanf格式字符串中的常量文本不匹配的字符而失败,则应该为您提供不匹配的字符以及后面的一些字符.如果它失败了,因为它试图读取一个整数并看到它不喜欢的东西,那么你应该得到它不能解释为整数的一部分的第一个字符以及下面的一些内容.等等.
| 归档时间: |
|
| 查看次数: |
1394 次 |
| 最近记录: |