zoukankan      html  css  js  c++  java
  • C IO programming test code

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <time.h>
    #define BUFLEN 255
    
    #define ERR_EXIT(m) 
    	do 
        { 
    		perror(m); 
    		exit(EXIT_FAILURE); 
    	} while(0)
    
    
    int main(){
    	int fd;
    	int fd_log;
    	char *buf01="open /dev/urandom success
    ";
    	char *buf02="open /dev/urandom failed
    ";
    	char dest[100];
    	int size;
    	int max=604800;//7days
    	int i=0;
    	time_t timep;
    	char tmpBuf[BUFLEN];
    
    	umask(0);
    	fd_log = open("/var/log/test_urandom.log",O_CREAT|O_APPEND|O_RDWR,0666);
    	if (fd_log == -1){
    		ERR_EXIT("open error");
    	}else{
    		printf("open /var/log/test_urandom.log success");
    	}
    
    	while(i < max){
    		fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
    
    		//now
    		time(&timep);
    		strcpy(tmpBuf,ctime(&timep));
    
    		//failed
    		if (fd < 0){
    			strcpy(dest,tmpBuf);
    			strcat(dest,":");
    			strcat(dest,buf02);
    			if((size=write(fd_log,dest,strlen(dest))) < 0){
    				perror("write failed");
    			}
    		}else{ //success
    			strcpy(dest,tmpBuf);
    			strcat(dest,":");
    			strcat(dest,buf01);
    			if ((size=write(fd_log,dest,strlen(dest))) < 0){
    				perror("write failed");
    			}
    		}
    		close(fd);
    		sleep(1);
    		i+=1;
    	}
    		close(fd_log);
    
    	return 0;
    }
    
  • 相关阅读:
    L2-011 玩转二叉树 二叉树
    L2-010 排座位 并查集
    L2-009 抢红包
    VS 编译报错:意外的字符
    关于js的类型转换
    github相关操作总结
    关于时间的相关处理
    uniapp实现简单的动画效果(不使用dom操作)
    uniapp选择日期
    vue使用音频组件
  • 原文地址:https://www.cnblogs.com/muahao/p/7172094.html
Copyright © 2011-2022 走看看