当前位置: 技术问答>linux和unix
有关字符数据写入文件的问题
来源: 互联网 发布时间:2016-03-28
本文导语: #include #include #include #include #include #include #include int main(int argc, char * argv[] ) { int i,fd; unsigned char str[]={0x0B,0x0C,0x11,0x00,0x04,0x00,0x32,0x00,0x00,0x12,0x00,0xF6,0x00,0x62,0x56}; fd = open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777); fw...
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char * argv[] )
{
int i,fd;
unsigned char str[]={0x0B,0x0C,0x11,0x00,0x04,0x00,0x32,0x00,0x00,0x12,0x00,0xF6,0x00,0x62,0x56};
fd = open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);
fwrite(&str,sizeof(str),1,fd);
fclose(fd);
}
编译的时候是通过的,但是一运行这个程序,就会出现段错误
我用gdb调试,出现这样的错误:Program received signal SIGSEGV, Segmentation fault
说是 fwrite()出错
ps:程序中写成fwrite(str,sizeof(str),1,fd);也是一样的错误
我就是想把这样的字符数据写到文件中去,哪里错了呢?
#include
#include
#include
#include
#include
#include
int main(int argc, char * argv[] )
{
int i,fd;
unsigned char str[]={0x0B,0x0C,0x11,0x00,0x04,0x00,0x32,0x00,0x00,0x12,0x00,0xF6,0x00,0x62,0x56};
fd = open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);
fwrite(&str,sizeof(str),1,fd);
fclose(fd);
}
编译的时候是通过的,但是一运行这个程序,就会出现段错误
我用gdb调试,出现这样的错误:Program received signal SIGSEGV, Segmentation fault
说是 fwrite()出错
ps:程序中写成fwrite(str,sizeof(str),1,fd);也是一样的错误
我就是想把这样的字符数据写到文件中去,哪里错了呢?
|
文件内容包含了不可见字符。
0x0B,0x0C,0x11,0x00,0x04,0x00,0x32,0x00,0x00,0x12,0x00,0xF6,0x00,0x62,0x56
改成
0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f
再看看
0x0B,0x0C,0x11,0x00,0x04,0x00,0x32,0x00,0x00,0x12,0x00,0xF6,0x00,0x62,0x56
改成
0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f
再看看
|
fwrite参数错误
fwrite 最后参数是FILE* 类型的
你是用open打开的,对应的写是write
fwrite 最后参数是FILE* 类型的
你是用open打开的,对应的写是write