文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>文件I/O 函数

文件I/O 函数

时间:2010-04-29  来源:blackgenius

这一部分是文件操作函数,自己放这里,以后可能能够用的上,一步步积累自己的函数,以后要用稍微改改o(∩_∩)o


/***************************************************
 * file name: pubfun_I_O.c creat on 29-4-2010      *
 * ----------------------------------------------- *
 * function: a collection of the I/O handle funct- *
 * ions.                                           *
 * ------------------------------------------------*
 * author: Wayne Lee company: Congine. Suzhou.     *
 * *************************************************/


#include <math.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>

#define MEM_MAX 1024
#define GATE 8388608L
#define STR_MAX 200


/* statement of the string handle functions */

int rec_log(char*, int, const char*, ...); //record the error message anytime there exits errors.


//record the error message anytime there exits errors.


// the start of the rec_log function.


int rec_log(char* fn, int lin, const char* fmt, ...) {

  long fd;
  char cpid[MEM_MAX];
  char line[MEM_MAX];
  char lp[MEM_MAX];
  char argbuf[MEM_MAX];
  char cmdbuf[MEM_MAX];
  
  time_t timep;
  struct tm *p;
  struct stat fst;

  va_list ap;

  memset(lp, 0x00, sizeof(lp));
  memset(argbuf, 0x00, sizeof(argbuf));
  memset(cmdbuf, 0x00, sizeof(cmdbuf));
  memset(line, 0x00, sizeof(line));
  sprintf(line, "%d", lin);

  va_start(ap, fmt);
  vsprintf(argbuf, fmt, ap);
  va_end(ap);

  time(&timep);
  p = localtime(&timep);

  strcat(lp, "/home/bsp/train2010/liw/log/err_d.log");
  sprintf(cmdbuf, "mv /home/bsp/train2010/liw/log/err_d.log /home/bsp/train2010/liw/log/err_d[%d%d%d%d%d%d].log", (1990+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);

  sprintf(cpid, "%ld", getpid());
  
  fd = open(lp, O_RDWR | O_APPEND | O_CREAT, 0666);
  if (fd == -1) {
    printf("Sorry for failed open! the error is: [%s]\n", strerror(errno));
    exit(1);
  }

  write(fd, "--------------------------------------------------\n", 51);
  write(fd, &cpid, strlen(cpid));
  write(fd, "\t", sizeof("\t"));
  write(fd, fn, strlen(fn));
  write(fd, "\t", sizeof("\t"));
  write(fd, line, strlen(line));
  write(fd, "\n", sizeof("\n"));
  write(fd, argbuf, sizeof(argbuf));
  write(fd, "\n--------------------------------------------------\n", 52);
     
  stat(lp, &fst);
  if (fst.st_size > GATE - 512) {
    system(cmdbuf);
  }
  close(fd);
  return 0;
}



相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载