文件IO
文件IO指的是对单个文件的操作,如打开文件、读取文件内容、写入文件、关闭文件等。这些操作通常由标准库(如C语言中的stdio.h
)或操作系统API提供。
常见的文件IO操作包括:
- 打开文件:使用
fopen
、open
等函数打开文件,并返回一个文件描述符或文件指针。 - 读取文件:使用
fread
、fgets
、fscanf
等函数从文件中读取数据。 - 写入文件:使用
fwrite
、fputs
、fprintf
等函数向文件中写入数据。 - 关闭文件:使用
fclose
、close
等函数关闭文件,释放相关资源。 - 移动文件指针:使用
fseek
、rewind
等函数移动文件中的读写位置。 - 文件属性操作:如获取文件大小、修改文件时间戳等。
目录IO
目录IO涉及对目录(文件夹)的操作,如创建目录、删除目录、列出目录中的文件等。这些操作通常由操作系统提供的API或库函数实现。
常见的目录IO操作包括:
- 创建目录:使用
mkdir
、mkdirp
等函数创建新目录。 - 删除目录:使用
rmdir
等函数删除空目录。 - 列出目录:使用
readdir
、scandir
等函数读取目录中的文件和子目录列表。 - 更改目录:使用
chdir
函数改变当前工作目录。 - 获取目录信息:如获取目录的路径、检查目录是否存在等。
文件IO:
1. lseek
off_t lseek(int fd, off_t offset, int whence);
功能:
重新设定文件描述符的偏移量
参数:
fd:文件描述符
offset:偏移量
whence:
SEEK_SET 文件开头
SEEK_CUR 文件当前位置
SEEK_END 文件末尾
返回值:
成功返回当前偏移量
失败返回-1
目录IO:
1.mkdir
int mkdir(const char *pathname, mode_t mode);
功能:
创建目录文件
参数:
pathname:文件路径
mode:文件的权限
返回值:
成功返回0
失败返回-1
rwx rwx rwx
111 111 111
0777
r: 目录中是否能够查看文件
w: 目录中是否能够新建文件
x: 目录是否能够进入
2.rmdir
int rmdir(const char *pathname);
功能:
删除空目录文件
返回值:
成功返回0
失败返回-1
3.opendir
DIR *opendir(const char *name);
功能:
打开目录获得目录流指针
参数:
name:目录文件路径
返回值:
成功返回目录流指针
失败返回NULL
4.closedir
int closedir(DIR *dirp);
功能:
关闭目录流指针
5.readdir
struct dirent *readdir(DIR *dirp);
功能:
从目录流中读取下一个目录项的结构体信息
参数:
dirp:目录流指针
返回值:
成功返回包含目录项信息的空间首地址
失败返回NULL
读到文件末尾返回NULL
struct dirent {
ino_t d_ino; /* Inode number */
off_t d_off; /* Not an offset; see below */
unsigned short d_reclen; /* Length of this record */
unsigned char d_type; /* Type of file; not supported
by all filesystem types */
char d_name[256]; /* Null-terminated filename */
};
6.chdir
int chdir(const char *path);
功能:
切换当前代码的工作路径
7.getcwd
char *getcwd(char *buf, size_t size);
功能:
获得当前目录的绝对路径
8.access
int access(const char *pathname, int mode);
功能:
检测调用函数的程序对文件是否拥有指定权限
参数:
pathname:文件路径
mode:
R_OK 检测是否拥有读权限
W_OK 检测是否拥有写权限
X_OK 检测是否拥有执行权限
F_OK 检测文件是否存在
返回值:
有该权限返回0
出错返回-1
遍历目录
#include "head.h"
int ListDir(const char *pdirname)
{
DIR *dp = NULL;
struct dirent *pp = NULL;
char tmpbuff[4096] = {0};
dp = opendir(pdirname);
if (NULL == dp)
{
perror("fail to opendir");
return -1;
}
while (1)
{
pp = readdir(dp);
if (NULL == pp)
{
break;
}
if ('.' == pp->d_name[0])
{
continue;
}
sprintf(tmpbuff, "%s/%s", pdirname, pp->d_name);
printf("%s\n", tmpbuff);
if (pp->d_type == DT_DIR)
{
ListDir(tmpbuff);
}
}
closedir(dp);
return 0;
}
int main(int argc, const char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage:./a.out dirname\n");
return -1;
}
ListDir(argv[1]);
return 0;
}
使用access函数检查文件是否存在
#include "head.h"
int main(int argc, const char *argv[])
{
int ret = 0;
if (argc != 2)
{
fprintf(stderr, "Usage:./a.out filename\n");
return -1;
}
ret = access(argv[1], F_OK);
if (0 == ret)
{
printf("该文件存在!\n");
}
else
{
printf("该文件不存在!\n");
}
return 0;
}
chdir与getcwd更改当前工作目录
#include "head.h"
int main(void)
{
char tmpbuff[4096] = {0};
getcwd(tmpbuff, sizeof(tmpbuff));
printf("tmpbuff = %s\n", tmpbuff);
chdir("..");
getcwd(tmpbuff, sizeof(tmpbuff));
printf("tmpbuff = %s\n", tmpbuff);
return 0;
}
#include "head.h"
typedef struct person
{
char name[8];
char birthday[12];
}person_t;
int main(void)
{
person_t a[100000];
int n = 0;
int i = 0;
int cnt = 0;
char maxvalue[12] = {"2014/09/06"};
char minvalue[12] = {"1814/09/06"};
int curmax = 0;
int curmin = 0;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%s%s", a[i].name, a[i].birthday);
}
for (i = 0; i < n; i++)
{
if (strcmp(a[i].birthday, maxvalue) <= 0 && strcmp(a[i].birthday, minvalue) >= 0)
{
cnt++;
if (1 == cnt)
{
curmax = curmin = i;
}
if (strcmp(a[i].birthday, a[curmax].birthday) > 0)
{
curmax = i;
}
if (strcmp(a[i].birthday, a[curmin].birthday) < 0)
{
curmin = i;
}
}
}
printf("%d %s %s\n", cnt, a[curmin].name, a[curmax].name);
return 0;
}
将源文件复制到目标文件中
#include "head.h"
int main(int argc, const char *argv[])
{
int fsrc = 0;
int fdst = 0;
char tmpbuff[4096] = {0};
ssize_t nret = 0;
if (argc != 3)
{
fprintf(stderr, "Usage:./a.out srcfilename dstfilename\n");
return -1;
}
fsrc = open(argv[1], O_RDONLY);
if (-1 == fsrc)
{
perror("fail to open");
return -1;
}
fdst = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (-1 == fdst)
{
perror("fail to open");
return -1;
}
while (1)
{
nret = read(fsrc, tmpbuff, sizeof(tmpbuff));
if (nret <= 0)
{
break;
}
write(fdst, tmpbuff, nret);
}
close(fsrc);
close(fdst);
#if 0
int i = 0;
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}
#endif
return 0;
}