关于收到的数据帧的处理!

小言言 2014-02-15 02:20:22
void packet_handler(u_char *dumpfp, const struct pcap_pkthdr *header, const u_char *pkt_data)
{ static int i=0;
printf("in packet handler\n");
if(i=0){
strcpy((char*)filename,(const char *)pkt_data+18);
i=1;
}
else
{/*处理一帧*/
savetofile(filename,pkt_data);
}
return;
}
void savetofile(u_char *filename,const u_char *pkt_data){
FILE *onfile;
if((onfile=fopen((char*)filename,"ab"))==NULL){
printf("文件打开失败");
exit(0);
}
fwrite(pkt_data+18,1,542,onfile);
fclose(onfile);
return ;
}
这段程序在对收到的数据帧进行及时处理,但是在文件打开那步就出错,打开失败,文件路径是从第一个数据帧里提取的,后面收到的数据存入这个文件,请教各位!
...全文
372 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhxianbin 2014-02-17
  • 打赏
  • 举报
回复
这错误,哎!! 以后用这种写法吧 if(0 == i)
sanredfish 2014-02-16
  • 打赏
  • 举报
回复
你的这句判定少写了一个等号,于是变成赋值,偏巧值为0,于是跳过了文件创建。
 if(i=0)
赵4老师 2014-02-16
  • 打赏
  • 举报
回复
引用 6 楼 sanredfish 的回复:
你的这句判定少写了一个等号,于是变成赋值,偏巧值为0,于是跳过了文件创建。
 if(i=0)
正解!
zhxianbin 2014-02-15
  • 打赏
  • 举报
回复
RETURN VALUE
       Upon successful completion fopen(), fdopen() and freopen() return a FILE pointer.  Otherwise, NULL is returned and errno is set  to
       indicate the error.

ERRORS
       EINVAL The mode provided to fopen(), fdopen(), or freopen() was invalid.

       The  fopen(),  fdopen()  and  freopen()  functions may also fail and set errno for any of the errors specified for the routine mal‐
       loc(3).

       The fopen() function may also fail and set errno for any of the errors specified for the routine open(2).

       The fdopen() function may also fail and set errno for any of the errors specified for the routine fcntl(2).

       The freopen() function may also fail and set errno for any of  the  errors  specified  for  the  routines  open(2),  fclose(3)  and
       fflush(3).
看看 errno
小言言 2014-02-15
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
void packet_handler(u_char *dumpfp, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    static int i=0;
    static u_char filename[256];
    printf("in packet handler\n");
    if(i=0) {
        strncpy((char*)filename,(const char *)pkt_data+18,255);filename[255]=0;
        i=1;
    } else {/*处理一帧*/
        savetofile(filename,pkt_data);
    }
    return;
}
void savetofile(u_char *filename,const u_char *pkt_data) {
    FILE *onfile;
    if((onfile=fopen((char*)filename,"ab"))==NULL) {
        printf("[%s]文件打开失败",filename);
        exit(0);
    }
    fwrite(pkt_data+18,1,542,onfile);
    fclose(onfile);
    return ;
}
不行啊。。。还是显示打开失败,昨天用这个还是可以的,只是存在丢包问题,今天不知道怎么的就不行了
小言言 2014-02-15
  • 打赏
  • 举报
回复
引用 1 楼 zhxianbin 的回复:
文件存在吗???
不存在的话不是会创建的吗?
赵4老师 2014-02-15
  • 打赏
  • 举报
回复
void packet_handler(u_char *dumpfp, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    static int i=0;
    static u_char filename[256];
    printf("in packet handler\n");
    if(i=0) {
        strncpy((char*)filename,(const char *)pkt_data+18,255);filename[255]=0;
        i=1;
    } else {/*处理一帧*/
        savetofile(filename,pkt_data);
    }
    return;
}
void savetofile(u_char *filename,const u_char *pkt_data) {
    FILE *onfile;
    if((onfile=fopen((char*)filename,"ab"))==NULL) {
        printf("[%s]文件打开失败",filename);
        exit(0);
    }
    fwrite(pkt_data+18,1,542,onfile);
    fclose(onfile);
    return ;
}
zhxianbin 2014-02-15
  • 打赏
  • 举报
回复
文件存在吗???

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧