美文网首页
linux应用程序,文件操作,获取时间等

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

作者: 嵌入式工作 | 来源:发表于2018-11-06 18:02 被阅读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();
    }

}

相关文章

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

    1获取系统时间 运行结果:test@ubuntu:~/test$ ./hhasctime :Tue Nov 6 ...

  • 文件描述符

    文件描述符是操作系统暴露给应用程序操作文件的句柄,Linux 称为 fd,windows 称 handle。 文件...

  • Unix 基础知识

    Unix 基础知识 @[执行新程序, 打开文件, 读取文件, 分配存储区, 获取当前时间等, 应用程序, shel...

  • linux 命令(持续更新ing ...)

    查看系统相关信息 直接作用于linux系统的相关操作 文件目录相关操作 磁盘操作 获取帮助 linux操作系统访问...

  • mac :NSWorkspace

    NSWorkspace 为应用程序提供如下服务: 1)打开,操作文件/设备,获取文件/设备信息 2)跟踪文件,设备...

  • MFC好的代码

    判断文件是否存在,并做相应的操作。 获取当前时间 获取最新的ID 文件操作//文件读写操作 移动窗口的位置

  • Linux内核、内存泄漏、软件测试相关知识点

    一、Linux你常用的指令 参考 二、Linux内核运行机制 参考文件 操作系统是应用程序和可用资源之间的媒介。 ...

  • Linux命令_文件目录管理

    Linux中一切皆是文件,文件有各种操作:创建、修改、复制、剪切、重命名、删除等。Linux系统中常用的与文件相关...

  • Linux文件操作

    文件操作 (Linux文件操作)) [文件|目录] Linux文件操作:为了对文件和目录进程处理,你需要用到系统...

  • Linux文件整体操作

    简介 介绍如何在Linux中创建、拷贝、移动、删除文件等的操作 文件整体操作 touch cp rm mv mkd...

网友评论

      本文标题:linux应用程序,文件操作,获取时间等

      本文链接:https://www.haomeiwen.com/subject/bsdmlftx.html