一、部分文件io和文件夹相关函数
1.lseek函数: off_t lseek(int fd, off_t offset, int whence)
2.用od -t x1(十六进制)文件名 看二进制文件
3.fopen open (补充)
w O_WRONLY | O CREAT |O_TRUNC
w+ O_RDWR | O_CREA | O_TRUNC
r O_RDONLY
r+ O_RDWR
a O_WRONLY | O_CREAT | O_APPEND
a+ O_RDWR | O_CREAT | O_APPEND
4. fdopen
FILE *fdopen(int fd, const char *mode)
关闭文件用fclose,封装度高
5.fileno函数:
6.perror()系统性报错的函数
参数里输入标识性信息eg:(“fopen main.c :10”)
要包<errno.h>头文件 适用范围:man2 man3
7.目录:(1)打开opendir(2)读取目录readdir (3)关闭closedir
-------DIR *dir------- (系统结构体)目录流指针
--------direntry *info---------
struct dirent *readdir(DIR *dirp)
包头文件#include<dirent.h>
8.把存储设备称为块设备,linux底层逻辑中,大致分为块设备和字符设备
DT_BLK 块设备 DT_CHR 字符设备
DT_DIR 目录、 DT_FIFO 管道。
windows桌面上放的东西叫做快捷方式,在liunx中叫做符号链接或软链接(DT_LNK)
DT_REG普通文件、DT_SOCK网络文件
9. time_t time (time _ t *tloc) 头文件<time.h>
man 3 localtime
struct tm *localtime(const time_t *time)
二、例题与练习
1.lseek函数
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int arcg, char **argv)
{
int fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(-1 == fd)
{
return 1;
}
off_t offset = lseek(fd, 1024 * 1024 * 10, SEEK_SET);
printf("%ld\n", offset);
write(fd, "b", 5);
close(fd);
return 0;
}
2.fileno.c函数
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
FILE *fp = fopen("1.txt", "w");
if(NULL == fp)
{
return 1;
}
int fd = fileno(fp);
write(fd, "hello", 5);
fclose(fp);
return 0;
}
3.fdopen.c函数
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd = open("1.txt", O_RDONLY);
if(-1 == fd)
{
return 1;
}
FILE *fp = fdopen(fd, "r");
if(NULL == fp)
{
return 1;
}
char buf[512] = {0};
fgets(buf, sizeof(buf), fp);
printf("%s", buf);
fclose(fp);
return 0;
}
4.perror.c函数
#include <stdio.h>
#include <errno.h> // extern int errno;
int main(int argc, char **argv)
{
perror("aaa");
FILE *fp = fopen("10.txt", "r");
if(NULL == fp)
{
printf("errno %d\n", errno);
perror("fopen main.c : 10");
return 0;
}
return 0;
}
5.time.c函数
#include <stdio.h>
#include <time.h>
int main(int argc, char **argv)
{
time_t tm;
tm = time(NULL);
printf("%ld\n", tm);
struct tm *tminfo = localtime(&tm);
printf("%d-%d-%d %d:%d:%d\n", tminfo->tm_year+1900,tminfo->tm_mon+1,tminfo->tm_mday
,tminfo->tm_hour,tminfo->tm_min,tminfo->tm_sec);
//system("pause");
return 0;
}
6./proc函数,计算数字文件的个数
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char **argv)
{
DIR *dir = opendir("/proc");
int count = 0;
while(1)
{
struct dirent *info = readdir(dir);
if(NULL == info)
{
break;
}
if(info->d_name[0] > 48 && info->d_name[0] <= 57)
{
count++;
}
}
printf("number file is %d\n", count);
closedir(dir);
return 0;
}
7.文件ls的递归
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
void open(char s[1024])
{
DIR *dir = opendir(s);
while(1)
{
struct dirent *info = readdir(dir);
if(NULL == info)
{
break;
}
if(strcmp(info->d_name, "..") == 0 || strcmp(info->d_name, ".") == 0)
{
continue;
}
else if (info->d_type == DT_DIR && strcmp(info->d_name, "..") && strcmp(info->d_name, "."))
{
char s1[1024 * 1024];
printf("----------------------------\n");
strcpy(s1, s);
strcat(s1, "/");
strcat(s1, info->d_name);
open(s1);
}
else
{
printf("%s\n", info->d_name);
}
}
closedir(dir);
}
int main(int argc, char **argv)
{
open("/home/linux/Linux biancheng");
//system("pause");
return 0;
}