#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#define MAXLINE 100
#define INIFILENAME "LOCAL_INFORM.INI"
char *
strepl (char *str)
{
char *i;
char *retu = (char *) malloc ((strlen (str) + 1) * sizeof (char));
memset (retu, '/0', strlen (str) * sizeof (char));
i = retu;
while (*str)
{
if (*str == ' ' || *str == '/n')
{
str++;
continue;
}
*retu = *str;
retu++;
str++;
}
retu = i;
return retu;
}
char *
GetIniFile (char * filename, char * field, char * item, char * def)
/*文件名*/ /*域*/ /*项目*/ /*缺省值*/
{
FILE *fp;
int count = 0;
char buf[MAXLINE];
char sfield[MAXLINE];
char sitem[MAXLINE];
static char *tmp;
memset (buf, '/0', sizeof (buf));
fp = fopen (filename, "rb");
if (fp == NULL)
{
return def; /*输出文件打开错*/
}
sprintf (sfield, "[%s]", field);
sprintf (sitem, "%s=", item);
while (fgets (buf, MAXLINE, fp) != NULL)
{
if (count == 2)
break;
switch (count)
{
case 0:
if (!(tmp = strstr (strepl (buf), sfield)) &&
count == 0)
continue;
else
{
if (strepl (buf)[0] == '[')
count = 1;
continue;
}
case 1:
if (!(tmp = strstr (strepl (buf), sitem)) &&
count == 1)
continue;
else
{
count = 2;
break;
}
default:
continue;
}
}
if (count != 2)
return def;
fclose (fp);
return tmp + strlen (sitem);
}
char * aaa()
{
char * z;
z =(char *) malloc(100);
strcpy(z,"hello");
return z;
}
char * bbb()
{
char * c;
c=aaa();
return c;
}
main()
{
char * p;
char cJgdm[10];
memset(cJgdm,'/0',sizeof(cJgdm));
p = (char *)GetIniFile(INIFILENAME, "LOCAL", "jgdm", NULL);
if( p!=NULL )
strcpy(cJgdm,p);
else
return -1;
printf("本地机构代码是 %s/n",cJgdm);
free(p);
}