IO进、线程——文件IO和时间函数

1.文件IO

最直观的系统调用

1.1打开文件

int open(const char *pathname, int flags, mode_t mode);

功能:打开/创建后打开一个文件
返回值:成功返回文件描述符,失败-1

0 —— 标准输入
1 —— 标准输出
2 —— 标准出错

参数说明
pathname:要打开的某个文件
flags:打开文件的方式

O_RDONLY:只读
O_WRONLY:只写
O_RDWR:读写
O_APPEND:在文件末尾追加(另起一行写)
O_CREAT:文件不存在则创建,文件存在则不管
O_EXCL:和O_CREAT,文件不存在则会创建,文件存在则直接报错
O_TRUNC:文件存在就清空

mode:创建文件时的权限,只有写了O_CREAT的时候才生效,如0666
最后文件的权限会使用 mode和~umask相与

参考代码
#include <fcntl.h>
#include <stdio.h>

int main() {
    // 打开文件,并以只写方式创建文件,权限设置为0644
    int fd = open("example.txt", O_CREAT | O_WRONLY, 0644);
    if (fd < 0) {
        perror("open");
        return -1;
    }

    // 写入内容到文件
    write(fd, "Hello, this is a file example!\n", 30);

    // 关闭文件
    close(fd);

    return 0;
}

1.2读文件

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t count);

功能:从fd里读取内容存放到buf
返回值:成功返回实际读到的字节数,失败返回-1
参数说明
fd:已经打开的文件描述符
buf:要存放的缓冲区
count:预计要读的字节数,不能超过buf的大小

参考代码
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    // 打开文件,并以只读方式打开
    int fd= open("example.txt", O_RDONLY | O_CREAT , 0644);
    if (fd < 0) {
        perror("open");
        return -1;
    }

    // 读取文件内容到缓冲区
    char buffer[256];
    ssize_t ret= read(fd, buffer, sizeof(buffer) - 1);
    if (ret < 0) {
        perror("read");
        return -1;
    }
    buffer[ret] = '\0'; // Null-terminate the buffer

    printf("Read %ld bytes: %s\n", bytesRead, buffer);

    // 关闭文件
    close(fd);

    return 0;
}
一种简单的图片加密方式
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
	int fd = open("二叉树遍历.png", O_RDWR);
	if(fd < 0){
		perror("open:");
		return -1;
	}
	char buf[2];
	read(fd, buf, 2);
	char temp = buf[0];
	buf[0] = buf[1];
	buf[1] = temp;

	lseek(fd, 0, SEEK_SET);
	write(fd, buf, 2);

#if 0
	do{
		ret = read(fd, buf, sizeof(buf));
	}while(ret < 0 && errno == EINTR);
#endif
}

1.3写文件

ssize_t write(int fd, const void *buf, size_t count);

功能:往fd里写内容
返回值:成功返回实际写入的字节数,失败返回-1
参数说明
fd:已经打开的文件描述符
buf:存放的要写入的内容的缓冲区
count:预计要写的字节数,不能超过buf的大小

read()和writ()实现copy功能

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {

    int fd_src = open("open.c", O_RDONLY);
    if(fd_src < 0){ 
        perror("open1");
        return -1; 
    }   
    int fd_dest = open("xxx", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if(fd_dest < 0){ 
        perror("open2");
        return -1; 
    }   

    char buf[64];
    int ret;
    while(1){
        memset(buf, 0, sizeof(buf));
        ret = read(fd_src, buf, sizeof(buf));
        if(ret <= 0){ 
            perror("read");
            return -1; 
        }

        write(fd_dest, buf, ret);
    }   

    // 关闭文件
    close(fd_src);

    return 0;
}

2.时间函数

#include <time.h>

time_t time(time_t *tloc);

功能:统计现在的系统时间(从1970-1-1 00:00:00到现在所过的秒数)
返回值:成功就返回这个秒数,失败返回-1
参数说明
tloc:用于存放这个秒数的变量地址

time_t tm;
tm = time(NULL);  <==>	time(&tm);
struct tm {
           int tm_sec;    /* Seconds (0-60) */
           int tm_min;    /* Minutes (0-59) */
           int tm_hour;   /* Hours (0-23) */
           int tm_mday;   /* Day of the month (1-31) */
           int tm_mon;    /* Month (0-11) */
           int tm_year;   /* Year - 1900 */
           int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
           int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
           int tm_isdst;  /* Daylight saving time */
};

①gmtime()

struct tm *gmtime(const time_t *timep);

功能:将统计的秒数转换成时间结构体的形式
返回值:成功返回时间结构体的地址,失败返回NULL
参数说明
timep:time()的返回值

// 获取当前系统时间
    time_t currentTime;
    time(&currentTime);
    // 转换为GMT时间
    struct tm *timeInfo = gmtime(&currentTime);

    // 打印GMT时间
    printf("GMT time: %s", asctime(timeInfo));

②localtime()

struct tm *localtime(const time_t *timep);

功能:将统计的秒数转换成时间结构体的形式
返回值:成功返回时间结构体的地址,失败返回NULL
参数说明
timep:time()的返回值

// 获取当前系统时间
    time_t currentTime;
    time(&currentTime);
	// 转换为本地时间
    struct tm *localTimeInfo = localtime(&currentTime);
    // 打印本地时间
    printf("Local time: %s", asctime(localTimeInfo));

③asctime()

char *asctime(const struct tm *tm);

功能:把系统时间按照固定格式转换成字符串
返回值:成功返回字符串的首地址,失败返回NULL
参数说明
tm:转换秒数后的时间结构体

// 获取当前系统时间
    time_t currentTime;
    time(&currentTime);
	// 转换为本地时间
    struct tm *localTimeInfo = localtime(&currentTime);
    // 打印本地时间
    printf("Local time: %s", asctime(localTimeInfo));

④ctime()

char *ctime(const time_t *timep);

功能:把系统时间按照固定格式转换成字符串
返回值:成功返回字符串的首地址,失败返回NULL
参数说明
timep:直接转换秒数到固定字符串格式

// 获取当前系统时间
    time_t currentTime;
    time(&currentTime);
	// 直接打印当前系统时间
    printf("Current time: %s", ctime(&currentTime));
参考代码
#include <stdio.h>
#include <time.h>

int main() {
    // 获取当前系统时间
    time_t currentTime;
    time(&currentTime);

    // 转换为GMT时间
    struct tm *timeInfo = gmtime(&currentTime);

    // 打印GMT时间
    printf("GMT time: %s", asctime(timeInfo));

    // 转换为本地时间
    struct tm *localTimeInfo = localtime(&currentTime);

    // 打印本地时间
    printf("Local time: %s", asctime(localTimeInfo));

    // 直接打印当前系统时间
    printf("Current time: %s", ctime(&currentTime));

    return 0;
}

在这里插入图片描述

3.文件属性

int stat(const char *pathname, struct stat *statbuf);

功能:获取文件的属性
返回值:成功返回0,失败返回-1
pathname:要查看的文件
statbuf:用于存放信息的结构体地址

struct stat {
     dev_t     st_dev;         /* 文件所在设备的ID */
     ino_t     st_ino;         /* 文件的inode号,用于唯一标识文件 */
     mode_t    st_mode;        /* 文件的类型和访问权限 */
     nlink_t   st_nlink;       /* 文件的硬链接数目 */
     uid_t     st_uid;         /* 文件所有者的用户ID */
     gid_t     st_gid;         /* 文件所有者的组ID */
     dev_t     st_rdev;        /* 如果文件是特殊文件(如设备文件),则表示设备的ID */
     off_t     st_size;        /* 文件的总大小,以字节为单位 */
     blksize_t st_blksize;     /* 文件系统I/O操作的块大小 */
     blkcnt_t  st_blocks;      /*  文件分配的512字节块数*/

	//struct timespec类型的结构体,用于表示时间:
    struct timespec st_atim;  /* 文件的最后访问时间 */
    struct timespec st_mtim;  /* 文件的最后修改时间 */
    struct timespec st_ctim;  /* 文件的最后状态改变时间 */

#define st_atime st_atim.tv_sec  /* 文件的最后访问时间(以秒为单位) */
#define st_mtime st_mtim.tv_sec  /* 文件的最后修改时间(以秒为单位) */
#define st_ctime st_ctim.tv_sec  /* 文件的最后状态改变时间(以秒为单位) */
};
参考代码
#include <stdio.h>
#include <sys/stat.h>

int main() {
    // 获取文件属性
    struct stat fileStat;
    if (stat("example.txt", &fileStat) == -1) {
        perror("stat");
        return 1;
    }

    // 打印文件属性
    printf("File size: %ld bytes\n", fileStat.st_size);
    printf("Owner UID: %d\n", fileStat.st_uid);
    printf("Group GID: %d\n", fileStat.st_gid);
    printf("Permissions: %o\n", fileStat.st_mode & 0777);

    return 0;
}

在这里插入图片描述

ls-a功能
#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[])
{ 
    DIR *dirp = opendir(".");
    if(dirp == NULL){
        perror("opendir:");
        return -1;
    }
    struct dirent *dp = NULL;
    while(1){
        dp = readdir(dirp);
        if(dp == NULL){
            break;
        }else if(dp->d_name[0] != '.'){
            printf("%s\t",dp->d_name);
        }
    }
    printf("\n");
        
    return 0;
} 
ls-l功能
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>

int main(int argc, char *argv[])
{ 
    if(argc < 2){
        printf("请输入路径%s <src>\n",argv[0]);
        return -1;
    }
    DIR* dirp = opendir(argv[1]);
    if(dirp == NULL){
        perror("opendir:");
        return -1;
    }
    struct dirent* dp = NULL;
    struct stat st;
    char pathname[1024];
    while((dp = readdir(dirp)) != NULL){
        if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
            continue;
        }
        sprintf(pathname, "%s/%s", argv[1], dp->d_name);
        if (lstat(pathname, &st) < 0) {
            perror("lstat:");
            break;
        }
        switch(st.st_mode & S_IFMT){
            case S_IFSOCK: printf("s");break;
            case S_IFLNK: printf("l");break;
            case S_IFREG: printf("-");break;
            case S_IFBLK: printf("b");break;
            case S_IFDIR: printf("d");break;
            case S_IFCHR: printf("c");break;
            case S_IFIFO: printf("p");break;
        }
        int n = 8;
        while(n > 0){
            if(st.st_mode & 1 << n){
                switch(n%3){
                    case 2:printf("r");break;
                    case 1:printf("w");break;
                    case 0:printf("x");break;
                }
            }else{
                printf("-");
            }
            n--;
        }
        struct passwd *u_uid = getpwuid(st.st_uid);
        printf(" %s",u_uid->pw_name);

        struct group* g_uid = getgrgid(st.st_gid);
        printf(" %s",g_uid->gr_name);

        printf(" %8ld",st.st_size);
        struct tm *time = localtime(&st.st_mtime);

        int month = time->tm_mon+1;
        switch(month)
        {
            case 1: printf(" 一月"); break;
            case 2: printf(" 二月"); break;
            case 3: printf(" 三月"); break;
            case 4: printf(" 四月"); break;
            case 5: printf(" 五月"); break;
            case 6: printf(" 六月"); break;
            case 7: printf(" 七月"); break;
            case 8: printf(" 八月"); break;
            case 9: printf(" 九月"); break;
            case 10: printf(" 十月"); break;
            case 11: printf(" 十一月"); break;
            case 12: printf(" 十二月"); break;
        }
        printf(" %2d %d:%02d  %s",time->tm_mday,time->tm_hour,time->tm_min,dp->d_name);
        printf("\n");

    }
    closedir(dirp);

    return 0;
} 
ls-l功能源文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>

void printPermissions(mode_t mode) {
    printf((S_ISDIR(mode)) ? "d" : "-");
    printf((mode & S_IRUSR) ? "r" : "-");
    printf((mode & S_IWUSR) ? "w" : "-");
    printf((mode & S_IXUSR) ? "x" : "-");
    printf((mode & S_IRGRP) ? "r" : "-");
    printf((mode & S_IWGRP) ? "w" : "-");
    printf((mode & S_IXGRP) ? "x" : "-");
    printf((mode & S_IROTH) ? "r" : "-");
    printf((mode & S_IWOTH) ? "w" : "-");
    printf((mode & S_IXOTH) ? "x" : "-");
}

void printFileInfo(const char *filename) {
    struct stat fileStat;

    if (stat(filename, &fileStat) < 0) {
        perror("stat");
        return;
    }

    // 打印文件权限
    printPermissions(fileStat.st_mode);
    printf(" ");

    // 打印硬链接数
    printf("%ld ", fileStat.st_nlink);

    // 打印所有者用户名
    struct passwd *pw = getpwuid(fileStat.st_uid);
    printf("%-2s ", pw->pw_name);

    // 打印所有者所属组名
    struct group *gr = getgrgid(fileStat.st_gid);
    printf("%-2s ", gr->gr_name);

    // 打印文件大小
    printf("%5ld ", fileStat.st_size);

    // 打印最后修改时间
    struct tm *timeinfo;
    char timeString[80];
    timeinfo = localtime(&fileStat.st_mtime);
    strftime(timeString, sizeof(timeString), "%Y年%m月%d日 %H:%M", timeinfo);
    printf("%s ", timeString);

    // 打印文件名
    printf("%s\n", basename((char*)filename));
}

int main() {
    char cwd[1024];

    if (getcwd(cwd, sizeof(cwd)) == NULL) {
        perror("getcwd");
        return 1;
    }

    DIR *dir = opendir(cwd);
    if (!dir) {
        perror("opendir");
        return 1;
    }

    int blocksize = 0;

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        // 忽略以'.'开头的文件(隐藏文件)
        if (entry->d_name[0] == '.')
            continue;

        char path[PATH_MAX];
        snprintf(path, sizeof(path), "%s/%s", cwd, entry->d_name);

        struct stat fileStat;
        if (stat(path, &fileStat) < 0) {
            perror("stat");
            continue;
        }

        blocksize += fileStat.st_blocks;
    }

    closedir(dir);

    // 打印总用量(总块数)
    printf("总用量:%d\n", blocksize / 2);

    dir = opendir(cwd);
    if (!dir) {
        perror("opendir");
        return 1;
    }

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_name[0] == '.')
            continue;

        char path[PATH_MAX];
        snprintf(path, sizeof(path), "%s/%s", cwd, entry->d_name);
        printFileInfo(path);
    }

    closedir(dir);

    return 0;
}

4.库的制作

4.1静态库

①.生成二进制文件
gcc -c linkstack.c -o linkstack.o

在这里插入图片描述

②.制作静态库文件(把.o文件转换成.a文件)
ar crs libmykun.a hello.o	//生成libmykun.a这个静态库文件

在这里插入图片描述

③.编译时链接
gcc linkstack_main.c -L. -llinkstack		//-L表示指定库路径,-l表示指定具体的库

在这里插入图片描述

4.2动态库

①.生成地址无关二进制文件
gcc -fPIC -c linkstack.c

在这里插入图片描述

②.制作动态库文件
gcc -shared -o liblinkstack.so linkstack.o

在这里插入图片描述

③.编译时链接
//-L表示指定库路径,-l表示指定具体的库
gcc linkstack_main.c -L. -llinkstack 

在这里插入图片描述

注:动态库程序运行时需要去默认路径加载库
1.把动态库文件拷贝到/lib或者/usr/lib目录下

2.配置动态库搜索文件
2.1、sudo vi /etc/ld.so.conf.d/my.conf(新建一个my.conf)将你的.so文件路径复制进去进行
2.2、把动态库路径存放进文件(再次刷新)
sudo ldconfig /etc/ld.so.conf.d/my.conf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小羊客栈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值