您的位置:首页 > 其它

从配置文件中获取IP,并分段显示

2011-03-21 23:04 369 查看
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "malloc.h"
#define N 20
#define M  256
typedef struct node
{
char name
;
char value
;
struct node *next;
}node;

void print(node *head)
{
node *p;
p=head;
if(p==NULL)
{
printf("Don't find the file !/n");
}
while(p)
{
printf("%s %s/n",p->name,p->value);
p=p->next;
}

}
void read_ip(node *head,char *ip0,char *ip1,char *ip2,char *ip3)
{
node *p;
int i = 0;
char *j[4];//指针数组
p = head;
while(p != NULL)
{
if((!strcmp(p->name,"IP")) || (!strcmp(p->name,"ip")))
{
//ip  10.10.19.166
//1第一段
for(j[0] = p->value;*j[0] != '.';j[0]++)
{
ip0[i++] = *j[0];
}
ip0[i] = '/0';
printf("%s/n",ip0);
//2第二段
i = 0;
for(j[1] = j[0]+1;*j[1] != '.';j[1]++)
{
ip1[i++] = *j[1];
}
ip1[i] = '/0';
printf("%s/n",ip1);
//3第三段
i = 0;
for(j[2] = j[1]+1;*j[2] != '.';j[2]++)
{
ip2[i++] = *j[2];
}
ip2[i] = '/0';
printf("%s/n",ip2);
//4第四段
i = 0;
for(j[3] = j[2]+1;*j[3] != '/0';j[3]++)
{
ip3[i++] = *j[3];
}
ip3[i-1] = '/0';
printf("%s/n",ip3);
break;
}
else
{
p = p->next;

}
}

}
node *read(char *filename)
{
node *head,*p1,*p2;
FILE *fp;
char aa[M],*j,*k;
int i, n = 0;
if((fp=fopen(filename,"r"))==NULL)
{
printf("===>Sorry,cannot open the file !/n");
exit(1);
}
while(fgets( aa, M, fp ) != NULL )
{
p1 =(node *)malloc(sizeof(node));
for(i = 0;aa[i] != '/0';i++)
{
if(aa[i] == ';')
{
break;
}
else
{
if(aa[i] == '=')
{
i=0;
break;
}
}
}
if(i==0)
{
for(k = aa; *k != '='; k++)
{
if(*k==' '||*k=='/t')
{
continue;
}
p1->name[i++]=*k;
}
p1->name[ i ] = '/0';
i=0;
for( j = k+1; *j != '/0'; j++ )
{
if( *j == ' ' || *j == '/t' )
{
continue;
}
p1->value[ i++] = *j;

}
p1->value[ i ] = '/0';
if(n==0 )
{
head = p2 = p1;
n++;
}
p2->next = p1;
p2 = p1;
}

}

p2->next=NULL;
fclose( fp );
return head;
}

int main(int argc, char* argv[])
{
node *head;
char *filename = "d://tianmo.txt";//配置文件存放目录
head = read(filename);
print(head);
char ip0[4],ip1[4],ip2[4],ip3[4];
read_ip(head,ip0,ip1,ip2,ip3);
printf("ip0 = %s/nip1 = %s/nip2 = %s/nip3 = %s/n/n",ip0,ip1,ip2,ip3);
return 0;
}


读取配置文件:

#define MAXLINE 1024
#define MAXHOST 32
#define MAXLEN  128

typedef strcut fData{
char host[MAXHOST];
char dir[MAXLEN];
struct fData * next;
}FNode;

FNode *readcfg(char *filename)
{
FILE *fp;
FNode *head,*p1,*p2;
char buf[MAXLINE],*s,*t,*k;

int i;

if((fp=fopen(filename,"rt"))==NULL)
{
printf("cannot open the file !\n");

exit(1);
}

while( !feof( fp ) )
{

p1 = (FNode *)malloc(sizeof(FNode));

fgets( buf, MAXLINE, fp );

s = strchr( buf, '/' );

if( s )
{
t = s + 1;

*s = '\0';

i = 0;

for( k = buf; *k != '\0'; k++)
{
if( *k == ' ' || *k == '\t' )
{
continue;
}
p1->host[ i++ ] = *k;

}
p1->host[ i ] = '\0';

i = 0;

for( k = t; *k != '\0'; k++ )
{
if( *k == ' ' || *k == '\t' )
{
continue;
}
p1->dir[ i++ ] = *k;

}
p1->dir[ i ] = '\0';

if( NULL == head )
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;

}

p1->next=NULL;

}
fclose( fp );

return head;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  null struct fp file include