1.文件分类
b c
d - l
s p
2.目录文件的操作:
1. mkdir
int mkdir(const char *pathname, mode_t mode);
功能:
创建目录
参数:
pathname:目录文件的路径
mode:目录文件的权限
返回值:
成功返回0
失败返回-1注意:
1.r 决定是否目录下可以查看其余文件信息
2.w 决定目录下的文件是否能够新建
3.x 决定目录文件是否能够进入
2.rmdir
int rmdir(const char *pathname);
功能:
删除空目录文件
参数:
pathname:
目录文件的路径
返回值:
成功返回0
失败返回-1
3.getcwd
char *getcwd(char *buf, size_t size);
功能:
获得当前工作目录的绝对路径
参数:
buf:存放路径空间首地址
size:最大存放字符的长度
返回值:
成功返回存放路径字符串空间首地址
失败返回NULL
4.chdir
int chdir(const char *path);
功能:
切换程序当前的工作路径
参数:
path:目的路径
返回值:
成功返回0
失败返回-1
5.chmod
chmod 0777 filename
chmod 0664 filename
chmod +/-r/w/x filename
int chmod(const char *pathname, mode_t mode);
功能:
修改文件权限
参数:
pathname:文件路径
mode:权限值 0777 0664
返回值:
成功返回0
失败返回-1
3.目录文件的读取:
1.操作方法: 打开目录
读取目录项
关闭目录
函数接口:
1.opendir
DIR *opendir(const char *name);
功能:
打开目录
参数:
name:目录文件路径
返回值:
成功返回目录流指针
失败返回NULL
2.closedir
int closedir(DIR *dirp);
功能:
关闭目录流指针
参数:
dirp:目录流指针
返回值:
成功返回0
失败返回-1
3.readdir
struct dirent *readdir(DIR *dirp);
功能:
读取目录项
参数:
dirp:目录流指针
返回值:
成功返回目录项指针
失败返回NULL
读到末尾返回NULL
struct dirent {
ino_t d_ino; /* Inode号,表示文件的索引节点号 ,用于唯一标识文件*/
off_t d_off; /* 偏移量,不是真正的偏移量,是一个内部使用的值,用来 标记目录项在目录流中的位置。
unsigned short d_reclen; /* 记录的长度,即该目录项的长度 */
unsigned char d_type; /* 文件类型,不是所有文件系统都支持 ,例如,普通 文 件、目录、符号链接等*/
char d_name[256];/* 以空字符结尾的文件名 */
};
路径: ../file.txt
文件名:file.txt
4.链接文件:
1.软链接(符号链接)
ln -s 要链接向的文件名 软链接文件名
ln -s b.txt a.txt
a.txt -> b.txt通过文件名进行链接
普通文件:
文件名 -> inode -> 数据块
软链接:
软连接文件名 -> inode -> 数据块 -> 链接向的文件名 -> inode -> 数据块int symlink(const char *target, const char *linkpath);(源,目的)
功能:
创建一个linkpath的软连接文件,里面存放target字符串
参数:
target:链接向的文件名
linkpath:软链接文件名
返回值:
成功返回0
失败返回-1
5.stat和lstat
int lstat(const char *pathname, struct stat *statbuf);
功能:
获得pathname对应文件的详细信息
参数:
pathname:文件路径
statbuf:存放文件详细信息空间的首地址
返回值:
成功返回0
失败返回-1
#include "../head.h"
int DisplayFile(char *pfilepath)
{
struct stat buf;
int ret = 0;
ret = lstat(pfilepath, &buf);
if (-1 == ret)
{
return -1;
}
switch (buf.st_mode & S_IFMT)
{
case S_IFREG:putchar('-');break;
case S_IFSOCK:putchar('s');break;
case S_IFLNK:putchar('l');break;
case S_IFBLK:putchar('b');break;
case S_IFDIR:putchar('d');break;
case S_IFCHR:putchar('c');break;
case S_IFIFO:putchar('p');break;
}
putchar(' ');
printf("%ld ", buf.st_nlink);
printf("%d ", buf.st_uid);
printf("%d ", buf.st_gid);
printf("%ld ", buf.st_size);
printf("%ld ", buf.st_mtime);
printf("%s\n", pfilepath);
return 0;
}
int main(void)
{
char filepath[256] = {0};
printf("请输入文件路径:\n");
scanf("%s", filepath);
DisplayFile(filepath);
return 0;
}
重点1 对文件的拷贝
1.文件io拷贝 1.利用read和write实现文件内容的拷贝(实现对一个图片的拷贝)
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int CopyFileContent(char *psrc, char *pdst)
{
int fsrc = -1;
int fdst = -1;
char tmpbuff[4096] = {0};
ssize_t nret = 0;
fsrc = open(psrc, O_RDONLY); //对二进制文件打开,文件io
if (-1 == fsrc)
{
return -1;
}
fdst = open(pdst, O_WRONLY | O_TRUNC | O_CREAT, 0664);//0664给的权限
if (-1 == fdst)
{
return -1;
}
while (1)
{
nret = read(fsrc, tmpbuff, sizeof(tmpbuff));//对文件读到temp开头的数组中
if (nret <= 0)
{
break;
}
write(fdst, tmpbuff, nret); //写入另一个文件
}
close(fsrc); //关闭文件描述符
close(fdst);
return 0;
}
int main(void)
{
char src[256] = {0};
char dst[256] = {0};
int ret = 0;
printf("请输入源文件名:\n"); //都是地址
gets(src);
printf("请输入目的文件名:\n");
gets(dst);
ret = CopyFileContent(src, dst);
if (0 == ret)
{
printf("拷贝文件成功!\n");
}
else
{
printf("拷贝文件失败!\n");
}
return 0;
}
2.普通文件二进制文件拷贝
#include <stdio.h>
int main(void)
{
FILE *fsrc = NULL;
FILE *fdst = NULL;
size_t nret = 0;
char tmpbuff[4096] = {0};
//打开源文件
fsrc = fopen("src.jpg", "r");
if (NULL == fsrc)
{
perror("fail to fopen");
return -1;
}
//打开目的文件
fdst = fopen("dst.jpg", "w");
if (NULL == fdst)
{
perror("fail to fopen");
fclose(fsrc);
return -1;
}
while (1)
{
nret = fread(tmpbuff, 1, sizeof(tmpbuff), fsrc);
if (0 == nret)
{
break;
}
fwrite(tmpbuff, 1, nret, fdst);
}
fclose(fsrc);
fclose(fdst);
return 0;
}
重点2 目录的遍历
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#endif
#include "../head.h"
int ListDir(char *pdirname)
{
char filepath[1024] = {0};
DIR *dp = NULL;
struct dirent *pp = NULL;
dp = opendir(pdirname);
if (NULL == dp)
{
return -1;
}
while (1)
{
pp = readdir(dp);
if (NULL == pp)
{
break;
}
if ('.' == *pp->d_name)
{
continue;
}
sprintf(filepath, "%s/%s", pdirname, pp->d_name);
printf("%s\n", filepath);
if (DT_DIR == pp->d_type)
{
ListDir(filepath);
}
}
closedir(dp);
return 0;
}
int main(void)
{
char dirname[256] = {0};
struct timeval start;
struct timeval end;
long ts;
printf("请输入目录路径:\n");
scanf("%s", dirname);
gettimeofday(&start, NULL);
ListDir(dirname);
gettimeofday(&end, NULL);
ts = (end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
printf("耗时 %ld.%ld s\n", ts / 1000000, ts % 1000000);
return 0;
}