Chinaunix首页 | 论坛 | 博客
  • 博客访问: 261961
  • 博文数量: 47
  • 博客积分: 1516
  • 博客等级: 上尉
  • 技术积分: 507
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-29 09:23
文章分类

全部博文(47)

文章存档

2009年(1)

2008年(3)

2007年(1)

2006年(36)

2005年(6)

分类: C/C++

2008-01-07 10:54:44

#ifndef __HEADFILE_H
#define __HEADFILE_H
#include
#include
#include
#include
#include
char *ReadConfigFile(char *openfile, char *conf_name[], char *conf_argv[], int conf_number, char *conf_hh);
char *OutLog(char *out_file, char *out_type, char *out_connect);
/**************************************************************************
*函数名称:写LOG文件                                                      *
*鸟语名称:OutLog                                                         *
*参数:log文件名(包括路径),输出信息类型,输出信息内容                  *
*                                                                         *
*使用方式:funcion_name("outfilename",out_type,out_connect)                *
*版本:1.0.0                                                              *
*作者:MaBin                                                              *
*日期: 2008-01-05                                                         *
*已知BUG:无                                                              *
*输出文件格式符合告警信息采集程序采集                                     *
*错误判断:返回值如果为NULL,则写LOG文件失败                              *
**************************************************************************/
char *OutLog(out_file, out_type, out_connect)
char *out_file;
char *out_type;
char *out_connect;
{
 if((out_file==NULL)||(out_type==NULL)||(out_connect==NULL))
 {
  return(NULL);
 }
 char *out_time;
 out_time=malloc(20);
 time_t timep;
 struct tm *out_time_tmp;
 time(&timep);
 out_time_tmp=gmtime(&timep);
 char *format="%Y%m%d %H:%M:%S";
 strftime(out_time, 20,format,out_time_tmp);
 FILE *out_log;
 if((out_log=fopen(out_file,"a")) == NULL)
 {
  return(NULL);
 }
 else
 {
  fprintf(out_log,"%s,%s,%s\n",out_time,out_type,out_connect);
  fclose(out_log);
  free(out_time);
  return("0");
 }
}
/**************************************************************************
*函数名称:读取配置文件                                                   *
*鸟语名称:ReadConfigFile                                                 *
*参数:配置文件名(可包括路径),配置名称,返回的值,读取配置的数量,     *
*      分割配置名称和值的符号(支持多种符号)                             *
*使用方式:funcion_name("filename",conf_name,conf_argv,int conf_num,"= \n")*
*版本:1.0.0                                                              *
*作者:MaBin                                                              *
*日期: 2008-01-05                                                         *
*已知BUG:无法判断返回的conf_argv值是否正确,在文件中有较多空格会造成返回 *
*   值不正确.                                                       *
*错误判断:返回值如果为NULL,则对conf_argv操作失败                        *
**************************************************************************/
char *ReadConfigFile(openfile, conf_name,conf_argv,conf_number,conf_hh)
char *openfile;
char *conf_name[];
char *conf_argv[];
int conf_number;
char *conf_hh;
{
 int i;
 for (i=0;i < conf_number;i++)
 {
  conf_argv[i]=malloc(255);
 }
 for (i=0;i < conf_number;i++)
 {
  if(conf_name[i]==NULL)
  {
   return(NULL);
  }
 }
 FILE *conf_file_name;
 if ((conf_file_name=fopen(openfile,"r")) == NULL)
 {
  printf("文件%s没有找到\n",openfile);
  return(NULL);
 }
 else
 {
  //进行读取操作
  char *tmp[conf_number];
  char *line_string;
  line_string=malloc(255);
  for (i=0;i < conf_number;i++)
  {
   tmp[i] = malloc(255);
   tmp[i]=NULL;
   fseek(conf_file_name,0,SEEK_SET);
   while(fgets(line_string,255,conf_file_name)!=NULL)
   {
    if(strcmp(conf_name[i],strtok(line_string,conf_hh)) == 0 )
    {
     tmp[i] = strtok(NULL,conf_hh);
     if(strlen(strcpy(conf_argv[i],tmp[i])) == 0 )
     {
      //不正常退出
      free(line_string);
      line_string=NULL;
      for (i=0;i < conf_number;i++)
      {
       free(tmp[i]);
       tmp[i]=NULL;
      }
      fclose(conf_file_name);
      return(NULL);
     }
    }
   }
  }
  free(line_string);
  line_string=NULL;
  for (i=0;i < conf_number;i++)
  {
   free(tmp[i]);
   tmp[i]=NULL;
  }
 fclose(conf_file_name);
 return("1");
 }
}
 
#endif //_HEADFILE_H
 
使用示列:
#include
#include "include/headfile.h"
#define MAX_STRING 255
#define CONF_NUM 4
char *G_conf_argv[CONF_NUM];
char *G_conf_name[CONF_NUM]={"dbname","code","password","user"};
int main()
{
 int i;
 printf("程序开始运行\n");
 printf("%d\n",sizeof(char));
 for (i=0;i {
  G_conf_argv[i]=malloc(MAX_STRING);
  //printf("%s\n",G_conf_argv[i]);
  G_conf_argv[i]=NULL;
 }
 if(ReadConfigFile("../etc/test.conf",G_conf_name,G_conf_argv,CONF_NUM,"= \n")==NULL)
 {
  printf("读取配置失败!!!\n");
  exit(1); 
 }
 else
 {
  for (i=0;i  {
   printf("%s的值为%s\n",G_conf_name[i],G_conf_argv[i]);
  }
 }
 if((OutLog("../log/test.log","INFO","程序开始运行。哈哈后"))==NULL)
 {
  printf("LOG文件写入失败!\n");
 }
 exit(0);
}
阅读(3122) | 评论(7) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-01-09 22:47:12

www.uusohu.com/mmovie 美-@女-@劈@-开@-腿@-被-@别-人@-猛-@干@--进@-免--@费--@观-@--看 www.uusohu.com/mmovie 美-@女-@劈@-开@-腿@-被-@别-人@-猛-@干@--进@-免--@费--@观-@--看

chinaunix网友2008-01-07 17:55:21

你一天就和代码打交道,肯定了啊

chinaunix网友2008-01-07 15:12:45

哈哈!还是我写的工整些^:)^

chinaunix网友2008-01-07 15:07:30

乱分配和使用内存,造成泄露和重复释放,根本不知道怎么用指针:)

chinaunix网友2008-01-07 15:06:16

int main(int argc, char* argv[]) { //_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); int i; char ch; printf("程序开始运行\n"); printf("%d\n",sizeof(char)); if(ReadConfigFile("a.log",G_conf_name,G_conf_argv,CONF_NUM,"= \n")==NULL) { printf("读取配置失败!!!\n"); exit(1); } else { for (i=0;i