linux应用程序,文件操作,获取时间等

2018-11-06  本文已影响0人  嵌入式工作

1获取系统时间

void get_local_time(void)
{
#include <time.h>
struct tm *ptr;
time_t lt;

lt = time(NULL);

ptr = localtime(&lt);

printf("asctime :%s\r\n",asctime(ptr));
printf("ctime:%s\r\n",ctime(&lt));

}

运行结果:
test@ubuntu:~/test$ ./hh
asctime :Tue Nov 6 01:55:25 2018

ctime:Tue Nov 6 01:55:25 2018

2: 创建文件

void creat_file(char*filename)
{
    if(creat(filename,0666)<0)
    {
    printf("creat file :%s err \r\n",filename);
    exit(EXIT_FAILURE);
    }else
    {
    printf("creat file %s success \r\n",filename);
    }   

}

3:文件拷贝

void file_cp(char*source,char*des_file)
{
FILE *s_fd;
FILE *d_fd;
long file_len;
char buf[BUFFER_SIZE];

//打开文件
s_fd=fopen(source,"rb");
d_fd=fopen(des_file,"wb");

//判断文件是否正确
if(s_fd==NULL)
{
printf("source file err %s \r\n",source);
exit(EXIT_FAILURE);
}else if(d_fd==NULL)
{
printf("目标文件错误:%s\r\n",des_file);
exit(EXIT_FAILURE);
}

//获取文件的size
/*get source file size,and set file fp to begin*/
fseek(s_fd,0,SEEK_END);
file_len=ftell(s_fd);
fseek(s_fd,0,SEEK_SET);
printf("file len:%d \r\n",file_len);

/*file cp*/
while(!feof(s_fd))
{
    fread(buf,BUFFER_SIZE,1,s_fd);
    if(BUFFER_SIZE>=file_len)
    {
    fwrite(buf,file_len,1,d_fd);
    }else
    {
    fwrite(buf,BUFFER_SIZE,1,d_fd);
    file_len-=BUFFER_SIZE;
    }
    bzero(buf,BUFFER_SIZE);
}
fclose(s_fd);
fclose(d_fd);
printf("file cp success\r\n");
exit(0);
}

4综合程序

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFER_SIZE 1024

void get_local_time(void)
{
#include <time.h>
struct tm *ptr;
time_t lt;

lt = time(NULL);

ptr = localtime(&lt);

printf("asctime :%s\r\n",asctime(ptr));
printf("ctime:%s\r\n",ctime(&lt));

}


void creat_file(char*filename)
{
    if(creat(filename,0666)<0)
    {
    printf("creat file :%s err \r\n",filename);
    exit(EXIT_FAILURE);
    }else
    {
    printf("creat file %s success \r\n",filename);
    }   

}

void file_cp(char*source,char*des_file)
{
FILE *s_fd;
FILE *d_fd;
long file_len;
char buf[BUFFER_SIZE];
s_fd=fopen(source,"rb");
d_fd=fopen(des_file,"wb");
if(s_fd==NULL)
{
printf("source file err %s \r\n",source);
exit(EXIT_FAILURE);
}else if(d_fd==NULL)
{
printf("目标文件错误:%s\r\n",des_file);
exit(EXIT_FAILURE);
}

/*get source file size,and set file fp to begin*/
fseek(s_fd,0,SEEK_END);
file_len=ftell(s_fd);
fseek(s_fd,0,SEEK_SET);
printf("file len:%d \r\n",file_len);

/*file cp*/
while(!feof(s_fd))
{

    fread(buf,BUFFER_SIZE,1,s_fd);
    if(BUFFER_SIZE>=file_len)
    {
    fwrite(buf,file_len,1,d_fd);
    }else
    {
    fwrite(buf,BUFFER_SIZE,1,d_fd);
    file_len-=BUFFER_SIZE;
    }
    bzero(buf,BUFFER_SIZE);

}

fclose(s_fd);
fclose(d_fd);




printf("file cp success\r\n");
exit(0);




}
int main(int argc ,char*argv[])
{

    if(argc<2)
    {
    //perror("u do not input the filename ,pls try again!\r\n");
    //exit(EXIT_FAILURE);
    get_local_time();
    }else if(argc==2)
    {
    printf("paras:%d para1:%s PARA2:%s \r\n",argc,argv[0],argv[1]);
    creat_file(argv[1]);
    exit(EXIT_SUCCESS);
    }else if(argc == 3)
    {
    file_cp(argv[1],argv[2]);
    }else
    {
        get_local_time();
    }

}

上一篇 下一篇

猜你喜欢

热点阅读