1,unlink的临时文件
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
int main(void)
{
int fp = open("tempfile", O_CREAT | O_RDWR,0664);
if (fp == -1)
{
perror("open");
exit(1);
}
//删除临时文件file
int ret = unlink("tempfile");
// write file
write(fp, "hello\n", 5);
//重置文件指针
lseek(fp, 0, SEEK_SET);
// read file
char buf[24] = { 0 };
int len = read(fp, buf, sizeof(buf));
//将读出的内容 ,屏幕上
write(1, buf, len );
//close file
close(fp);
return 0;
}
unlink文档说明
UNLINK(2) Linux Programmer's Manual UNLINK(2)
NAME
unlink, unlinkat - delete a name and possibly the file it refers to
SYNOPSIS
#include <unistd.h>
int unlink(const char *pathname);
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
int unlinkat(int dirfd, const char *pathname, int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
unlinkat():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_ATFILE_SOURCE
DESCRIPTION
unlink() deletes a name from the filesystem. If that name was the last link
Manual page unlink(2) line 1 (press h for help or q to quit)
2,文件重命名 rename 函数
文档说明
NAME
rename, renameat, renameat2 - change the name or location of a file
SYNOPSIS
#include <stdio.h>
int rename(const char *oldpath, const char *newpath);
#include <fcntl.h> /* Definition of AT_* constants */
#include <stdio.h>
int renameat(int olddirfd, const char *oldpath,
int newdirfd, const char *newpath);
int renameat2(int olddirfd, const char *oldpath,
int newdirfd, const char *newpath, unsigned int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
renameat():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
Manual page rename(2) line 1 (press h for help or q to quit)
3,目录操作的函数
* 1,chdir 函数 *
修改当前进程的路径:函数原型: int chdir(const char *path);
* 2,getcwd 函数*
获取当前进程工作目录:函数原型: char *getcwd(char *buf, size_t size);
* 3, mkdir 函数*
int mkdir(const char *pathname, mode_t mode);
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
int mkdirat(int dirfd, const char *pathname, mode_t mode);
* 4, rmdir 函数*
删除一个空目录
int rmdir(const char *pathname);
DESCRIPTION
rmdir() deletes a directory, which must be empty.
* 5, opendir 函数*
打开目录
S
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
fdopendir():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
* 6, readdir函数*
readdir, readdir_r - read a directory
SYNOPSIS
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
readdir_r():
_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE ||
_POSIX_SOURCE
SCRIPTION
The readdir() function returns a pointer to a dirent structure representing
the next directory entry in the directory stream pointed to by dirp. It
returns NULL on reaching the end of the directory stream or if an error
occurred.
On Linux, the dirent structure is defined as follows:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* not an offset; see NOTES */
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]; /* filename */
};