file.c
上传用户:sdtbys
上传日期:2009-12-06
资源大小:13k
文件大小:2k
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define FileOpenFail -1401
- #define EnvFileFail -1402
- GetEnv(char *filename,char *envname,char *envval)
- {
- FILE *fp;
- char fileline[200];
- char fileenv[100];
- char fileenvval[100];
- fp=fopen(filename,"r");
- if (fp==NULL)
- { ShowMsg("FILE %s CAN NOT OPEN n",filename);
- return FileOpenFail;
- }
- while(!feof(fp))
- {
- if (fgets(fileline,200,fp)==NULL) break;
- if (strlen(fileline)!=0) fileline[strlen(fileline)-1]=' ';
- memset(fileenv,' ',sizeof(fileenv));
- memset(fileenvval,' ',sizeof(fileenvval));
- strdepart(fileline,'=',fileenv,fileenvval);
- if (strcmp(fileenv,envname)==0)
- {strcpy(envval,fileenvval);
- fclose(fp);
- return(0);
- }
- }
- return(0);
- }
- strdepart(char *a,char d,char *a1,char *a2)
- {
- int i=0,j=0;
- if (a[i]==' ') return(0);
- while(1)
- {
- if (a[i]==' ') break;
- if (a[i]==d) break;
- a1[i]=a[i];
- i++;
- }
- if (a[i]==' ')
- { ShowMsg("EnvFile Line: %s Not Correct!n",a);
- return(EnvFileFail);}
- strcpy(a2,a+i+1);
- }
- long GetFileSize(char *Filename)
- {
- FILE *fp;
-
- long lSize;
- fp=fopen(Filename,"r");
- if (fp==NULL) return (-1);
- fseek(fp,0,SEEK_END);
- /* fgetpos(fp,&lSize); */
- lSize=ftell(fp);
- fseek(fp,0,SEEK_SET);
- fclose(fp);
- return lSize;
- }
- /*
- char MsgLog[30]="./1.log";
- main()
- { long a;
- a=GetFileSize("./tmp51");
- printf("filesize=%dn",a);
- }*/
- /*
- main()
- { char ip[100];
- GetEnv("1.cfg","nj400_ipaddr",ip);
- printf("get ip= %s len=%dn",ip,strlen(ip));
- LookChar(ip,strlen(ip));
-
- }*/