在项目中我定义了一个结构体数组,头文件如下:

C/C++ code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef MY_TABLE_STRUCT_H
#define  MY_TABLE_STRUCT_H
 
#define  POLICY_UTIL_ARRAY_LENGTH   256*64
 
#define     ARRAY_SIZE 256
 
long saved_policy_id[10];
long saved_udp_allow_IP_ID[10];
long save_log_ID[10];
 
 
/*与日志审计相关的文件路径定义*/
 
#define SYSTEM_LOG   "/tmp/log/system.log"
#define HTTP_LOG   "/tmp/log/http.log"
#define SMTP_LOG   "/tmp/log/smtp.log"
#define POP3_LOG   "/tmp/log/pop3.log"
 
#define SYSTEM_INTERMEDIARY_LOG   "/tmp/log/system_intermediary.log"
#define HTTP_INTERMEDIARY_LOG  "/tmp/log/http_intermediary.log"
#define SMTP_INTERMEDIARY_LOG  "/tmp/log/smtp_intermediary.log"
#define POP3_INTERMEDIARY_LOG  "/tmp/log/pop3_intermediary.log"
 
 
typedef struct{
    int used; //0 for unused
    int AppID; 
    /*
           00 "系统",
           01 "HTTP应用",
        02 "SMTP应用",
        03 "POP3应用",
        04 "FTP应用",
        05 "TELNET应用",
        06 "NULL ",
        07 "用户自定义应用",
        08 "TCP空白协议",
        09 "UDP应用",
        0a "ORACLE数据库应用",
        0b "SQL Server 数据库应用",
        0c "邮件服务器与邮件服务器应用",
    */
    char SourceIP[20];
    short SourcePort;
    char DestIP[20];
    short DestPort;
    char SourceType; //I for inner;O for outter
    char IsDestProxyMode;
    short MaxConn;
    char IsWorking; 
            // 'Y' for "自动启用,现启用"
            //'N' for "自动启用,现停用"
            //'U' for "手动启用,现停用"
            //'W' for "手动启用,现启用"
       char IsAuth;
        // 0 for no auth
        // 1  for auth
    char name[20];
         
    UINT16 InnerPort; //for udp
    UINT16 OuterPort;
     
    char IsAllowed; //安全策略黑白指示
                  // 'Y' for 白
                  // 'N' for 黑
        char FromTime[8][8];
     char ToTime[8][8];
     char WeekDay[8][8];
 
 
     //for udp allowed ip
     unsigned int inner_udp_ip[256];
     unsigned int outer_udp_ip[256];
 
     //for tcp allowed ip
     struct IP {
      char ip[255];
      int    black_white;
         } allowed_ip[256];
       
     struct POLICY {
        long p_id;
         short type;
        char IsWorking;
        char p_content[255];
        char IsAllowed;
         } policy[64];     
}TABLE_CHANNEL;
 
TABLE_CHANNEL table_channels[ARRAY_SIZE];
 
#endif

将结构体数组中的数据保存到文件中,实现函数如下:

C/C++ code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void chn_write_file()
{
      FILE *fp;
    int i;
 
    DO_LOG("chn_write_file():write to the file start...\n");
     
    fp=fopen("/tmp/data/db_file/tcp_file","wb");
    if(fp==NULL)
    {
        printf("file /tmp/udp_chn open error\n");
        return;
    }
    //for(i=0;i<256;i++)
        //fwrite(&table_channels[i],sizeof(TABLE_CHANNEL),1,fp);//把内存中的信息写入到文件中去
         
    fwrite(table_channels, sizeof(TABLE_CHANNEL), 256, fp);
     
    fclose(fp);
     
    DO_LOG("chn_write_file():write to the file end...\n");
}

从文件中读取数据存储到结构体数组中,实现函数如下:

C/C++ code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
void chn_read_file()
{
    MY_DEBUG_LOG("chn_read_file():read from the file start...\n");
    DO_LOG("chn_read_file():read from the file start...\n");
     
    FILE *fp;
    int i, j;
     
    fp= fopen("/tmp/data/db_file/tcp_file""rb");
 
    if(fp == NULL)
    {
        DO_LOG("chn_read_file():open file failed, because:%s!\n"strerror(errno));
 
        return ;
    }
     
    /*for(i = 0; i< 256; i++)
    {
        fread(&table_channels[i], sizeof(TABLE_CHANNEL), 1, fp);
    }*/
 
    fread(table_channels, sizeof(TABLE_CHANNEL), 256, fp);
        /*****打印结构体数组中的数据*******/
    MY_DEBUG_LOG("chn_read_file(): The informations of approuteways bellows:\n");
     
       for(i =0; i<256; i++)
    {
        if(table_channels[i].used == 0)
         {
            continue;
        }
         
        MY_DEBUG_LOG("[ReadFile]:ID:%d, used:%d, AppID:%d, SourceIP:%s, SourcePort:%d,\
            DestIP:%s, DestPort:%d, SourceType:%c, IsDestProxyMode:%c, MaxConn:%d,\
            IsWorking:%c, IsAuth:%c, InnerPort:%d, OuterPort:%d, IsAllowed:%c, name:%s!\n", \
            i, table_channels[i].used, table_channels[i].AppID,table_channels[i].SourceIP,table_channels[i].SourcePort, \
            table_channels[i].DestIP, table_channels[i].DestPort,  table_channels[i].SourceType, table_channels[i].IsDestProxyMode,\
            table_channels[i].MaxConn,  table_channels[i].IsWorking,table_channels[i].IsAuth,table_channels[i].InnerPort,\
            table_channels[i].OuterPort,table_channels[i].IsAllowed,table_channels[i].name);
    }
 
    MY_DEBUG_LOG("chn_read_file(): The informations of allowd time bellows:\n");
     
     for(i=0; i<256; i++)
     {
 
        if(table_channels[i].used == 0)
         {
            continue;
        }
 
        for(j=0; j<8; j++)
        {   
            if(table_channels[i].FromTime[j][0] == 0)
            {   
                continue;
            }
             
            if(table_channels[i].ToTime[j][0] == 0)
            {   
                continue;
            }
             
            if(table_channels[i].WeekDay[j][0] == 0)
            {   
                continue;
            }
 
             
            MY_DEBUG_LOG("chn_read_file():ID:%d,FormTime:%s,ToTime:%s,WeekDay:%s\n",\
                i,&table_channels[i].FromTime[j][0], &table_channels[i].ToTime[j][0], &table_channels[i].WeekDay[j][0]);
        }
         
     }
 
     MY_DEBUG_LOG("chn_read_file(): The informations of allowd IPs bellows:\n");
 
     for(i=0; i<256; i++)
     {
 
        if(table_channels[i].used == 0)
         {
            continue;
        }
 
        for(j=0; j<256; j++)
        {
            if(table_channels[i].allowed_ip[j].ip[0] ==0)
            {
                continue;
            }
 
 
            if(table_channels[i].allowed_ip[j].black_white == 0)
            {
                MY_DEBUG_LOG("chn_read_file():ID:%d, ip %s, black_white:%d\n"
                    i, table_channels[i].allowed_ip[j].ip, table_channels[i].allowed_ip[j].black_white);
            }
        }
 
          
     }
 
 
       MY_DEBUG_LOG("chn_read_file(): The informations of allowd users bellows:\n");
    for(i=0; i<256; i++)
     {
 
        if(table_channels[i].used == 0)
         {
            continue;
        }
 
        for(j=0; j<256; j++)
        {
            if(table_channels[i].allowed_ip[j].ip[0] ==0)
            {
                continue;
            }
 
 
            if(table_channels[i].allowed_ip[j].black_white == 1)
            {
                MY_DEBUG_LOG("chn_read_file():ID:%d, users %s, black_white:%d\n"
                    i, table_channels[i].allowed_ip[j].ip, table_channels[i].allowed_ip[j].black_white);
            }
        }
 
          
     }
         
    MY_DEBUG_LOG("chn_read_file(): The informations of policies bellows:\n");
     
    for(i=0; i<256; i++)
     {
         if(table_channels[i].used == 0)
         {
                continue;
        }
 
        for(j =0 ; j <64; j++)
        {
            if(table_channels[i].policy[j].type == 0)
            {   
                continue;
            }
 
            MY_DEBUG_LOG("[ReadFile]:ID:%d, AppID:%d, used:%d, policy_id:%d , type:%x,  IsWorking:%c,  p_content:%s, IsAllowed:%c.\n"
                i,table_channels[i].AppID,table_channels[i].used,table_channels[i].policy[j].p_id,table_channels[i].policy[j].type,
                table_channels[i].policy[j].IsWorking,table_channels[i].policy[j].p_content, table_channels[i].policy[j].IsAllowed);
        }
     }
 
     
    fclose(fp);
 
    MY_DEBUG_LOG("chn_read_file():read from the file start end...\n");
     
}

现在出现问题,请教网上各位高手:
在一个程序中可以正确执行上面的两个操作,并用日志的形式打印数据,是正确的;可是在其它地方调用void chn_read_file()函数时候,并用日志形式打印数据,却显示的是错误的数据(具体错误:allowed_ip结构体,和policy结构体中的数据是错误的,随机生成的,也就是没从文件中正确读出来)函数我用的一样,为什么会出现这种情况?

C语言 结构体数组保存到二进制文件中的更多相关文章

  1. C语言之:结构体动态分配内存(利用结构体数组保存不超过10个学生的信息,每个学生的信息包括:学号、姓名和三门课(高数、物理和英语 )的成绩和平均分(整型)。)

    题目内容: 利用结构体数组保存不超过10个学生的信息,每个学生的信息包括:学号.姓名和三门课(高数.物理和英语 )的成绩和平均分(整型). 编写程序,从键盘输入学生的人数,然后依次输入每个学生的学号. ...

  2. C语言结构体数组遇上typedef

    昨天韩同学在做数据结构题的时候,问了我一个关于typedef 与结构体数组的问题: typedef struct vexnode { int vertex; arcnode* firstarc; }a ...

  3. c语言结构体数组定义的三种方式

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; int dangdang;//成员名可以和类名同名 }ddd[];/ ...

  4. C语言结构体数组内带字符数组初始化和赋值

    1.首先定义结构体数组: typedef struct BleAndTspRmtCmd{ char terminal[3]; char note[3]; char rmtCmd[10]; char c ...

  5. C语言结构体数组

    #include <stdio.h> int main() { /*************************************************** *结构体数组:数组 ...

  6. c语言结构体数组引用

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; }dbdd[]={{,,}, {,,} };//初始化 void m ...

  7. NumPy-快速处理数据--ndarray对象--多维数组的存取、结构体数组存取、内存对齐、Numpy内存结构

    本文摘自<用Python做科学计算>,版权归原作者所有. 上一篇讲到:NumPy-快速处理数据--ndarray对象--数组的创建和存取 接下来接着介绍多维数组的存取.结构体数组存取.内存 ...

  8. 读陈浩的《C语言结构体里的成员数组和指针》总结,零长度数组

    原文链接:C语言结构体里的成员数组和指针 复制例如以下: 单看这文章的标题,你可能会认为好像没什么意思.你先别下这个结论,相信这篇文章会对你理解C语言有帮助.这篇文章产生的背景是在微博上,看到@Lar ...

  9. C语言中的结构体,结构体数组

    C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...

随机推荐

  1. HTML5API___geolocation

    地理位置查询:geolocation window.navigator.geolocation 该对象下总共有3个方法 Geolocation {getCurrentPosition: functio ...

  2. .net-一般处理程序及生命周期

    IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHandler):是 ...

  3. 在unity中用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放

    在场景中添加一个Plan,Camera,Directional Light,Cube.添加两个脚本scrollerScirpt(挂在Camera),CubeDragScript(挂在Cube上). 1 ...

  4. SQL SERVER 2008 R2 自动备份并删除过期备份数据

        我们的系统维护的过程中肯定需要对数据库进行定期的备份,但是如果定时手工备份的话,不但浪费时间,也不能保证每次都可以按时备份,所以自动备份成为了我们的不二选择,但是定时备份需要定期清理备份文件, ...

  5. Android应用开发提高篇(5)-----Camera使用

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/06/2382679.html 一.概述 Camera是手机的一个很重要的设备,可以说现在的每一部 ...

  6. 别忘记给你博客的windows live writer配置 ping服务

    写好一篇博客,想要实现秒收.就必须要为文章添加ping服务. 这里介绍一下给wlw添加ping服务的办法. 点击工具---选项--ping服务器. 在右侧栏中加入以下地址 http://rpc.pin ...

  7. 用Mediawiki做百科网站资源大参考

    MediaWiki简易安装教程**关于mediawiki 一些好的资料: http://codex.wordpress.org.cn/Mediawiki%E5%BB%BA%E7%AB%99%E7%BB ...

  8. HDU 2136 Largest prime factor

    题目大意:求出比给出数小的互质的质数个数. 题解:直接用筛法求素数,稍微改编一下,将原先的布尔数组变为数组用来记录信息就可以了. 注意点:大的数组定义要放在程序的开头,不要放在main里面,不然会栈溢 ...

  9. Android测试TestSuite的执行方法

    public class StartTest extends InstrumentationTestRunner {         public  TestSuite getAllTests() { ...

  10. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 55 ...