1、文件描述符🔗
STDIN_FILENO 0 标准输入
STDOUT_FILENO 1 标准输出
STDERR_FILENO 2 标准错误
2、系统函数 open() close() 打开、关闭文件🔗
//该包含的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int open(const char* pathname, int flags);
//flags :
//O_RDONLY O_WRONLY O_RDWR O_CAREAT O_APPEND
int close(int fd);
3、显示出错信息🔗
#include <string.h>
#include <errno.h>
if(fd == -1)
{
//将error int 转换为error string
printf("%s\n",strerror(errno));
}
4、系统函数 read()、write()读写文件🔗
ssize_t read (int fd,void *buf,size_t count); //成功则返回读取的字节数,读到EOF返回0,失败返回-1 ssize_t write (int fd,void *buf,size_t count);
//读文件的代码
int main(int arg, char *args[])
{
if (arg < 2)
return 0;
int fd = open(args[1], O_RDONLY); //只读方式打开文件abc.txt
if (fd == -1)
{
printf("error is %s\n", strerror(errno));
} else
{
printf("success fd = %d\n", fd);
char buf[100];
memset(buf, 0, sizeof(buf));
while(read(fd, buf, sizeof(buf) - 1) > 0)//循环读取文件内容,直到文件结尾,退出循环
{
printf("%s\n", buf);
memset(buf, 0, sizeof(buf));
}
close(fd);
}
return 0;
}
//写文件的代码
int main(int arg, char *args[])
{
char s[] = "abc.txt";
int fd = open(s, O_RDWR | O_APPEND);//用读写追加方式打开文件
if (fd == -1)
{
printf("error is %s\n", strerror(errno));
}else
{
printf("success fd = %d\n", fd);
char buf[100];
memset(buf, 0, sizeof(buf));
strcpy(buf, "hello world\n");
int i = write(fd, buf, strlen(buf));//这里要用strlen函数
close(fd);
}
return 0;
}
5、fstat() stat() 的使用🔗
int fstat (int fd, struct stat * buf)
//参数fd必须是用open调用返回的有效文件描述符
int stat (const char* path, struct stat * buf)
//参数path必须是文件路径
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
std_mod成员:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
//得到文件状态代码
int main(int arg, char *args[])
{
int fd = open(args[1], O_RDONLY);
if (fd == -1)
{
printf("error is %s\n", strerror(errno));
}else
{
printf("success fd = %d\n", fd);
struct stat buf;
fstat(fd, &buf);
if (S_ISREG(buf.st_mode))//判断文件是否为标准文件
{
printf("%s is charfile\n", args[1]);
}
if (S_ISDIR(buf.st_mode))//判断文件是否为目录
{
printf("%s is dir\n", args[1]);
}
printf("%s size =%d\n", args[1], buf.st_size);//得到文件大小
close(fd);
}
return 0;
}
关闭回显:
char* getpass (const char *prompt)
//参数prompt为屏幕提示字符
//函数返回值为用户键盘输入的字符串
int main ()
{
char* phrase = getpass ("please input:");
printf("%s",phrase);
}
5、c语言库函数fopen()、fclose()打开、关闭文件🔗
FILE *p fopen (const char *path,const char * mode);
//fopen 以mode模式打开名为path的文件
//fopen 返回一个文件指针
//出现错误,fopen返回NULL,并把errno设置为恰当的值
//mode 说明:r r+ w w+ a a+
int fclose(FILE* stream);
6、c语言库函fread()、fwrite()数读写文件🔗
size_t fread(void *ptr,size_t size,size_t nmemb,FILE * stream);
size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE * stream);
//参数ptr指向缓冲区保存或读取的数据
//参数size 控制记录大小
//参数nmemb为记录数
//函数返回读取或回写的记录数
//原则上第二个参数*第三个参数不要大于第一个参数的buf【100】的大小
//C库函数读取文件的代码
int main(int arg, char *args[])
{
FILE *p = fopen(args[1], "r+");
if (p == NULL)
{
printf("error is %s\n", strerror(errno));
}else
{
printf("success\n");
char buf[100];
size_t rc = 0;
while(1)
{
size_t tmp = fread(buf, 1, sizeof(buf), p);//原则是第二个参数乘以第三个参数的大小不能超过缓冲区
rc += tmp;//求文件的大小
if (tmp == 0)
break;
}
printf("rc = %d\n", rc);
fclose(p);
}
return 0;
}
//c库函数读写二进制文件的代码
struct person
{
int id;
char name[20];
int age;
int sex;
char tel[20];
};
int Fwrite(int arg, char *args[])
{
FILE *p = fopen(args[1], "w");
if (p == NULL)
{
printf("error is %s\n", strerror(errno));
} else
{
printf("success\n");
struct person man[10];
memset(&man, 0, sizeof(man));
man[0].id = 0;
strcpy(man[0].name, "小明");
man[0].age = 50;
man[0].sex = 1;
strcpy(man[0].tel, "123");
man[1].id = 1;
strcpy(man[1].name, "小白");
man[1].age = 20;
man[1].sex = 0;
strcpy(man[1].tel, "321");
fwrite(&man, sizeof(struct person), 2, p);
fclose(p);
}
return 0;
}
int Fread(int arg, char *args[])
{
FILE *p = fopen(args[1], "w");
if (p == NULL)
{
printf("error is %s\n", strerror(errno));
} else
{
printf("success\n");
struct person man;
memset(&man, 0, sizeof(man));
while(fread(&man, sizeof(struct person), 1, p))
{
printf("id=%d\n", man.id);
printf("name=%s\n", man.name);
printf("age=%d\n", man.age);
printf("tel=%s\n", man.tel);
}
fclose(p);
}
return 0;
}
7、行输入和行输出🔗
char fgets(char *s,int size,FILE *stream);
int fputs(const char *s,FILEe *stream);
//fgets 从文件中读取一行,返回EOF代表文件结束
//fputs 向文件中写入一行
8、文件删除改名函数🔗
int remove (const char* pathname);
int rename(const char* oldpath,const char *newpath);
9、找到当前目录🔗
char * getcwd(char *buf,size_t size)
//getcwd函数把当前的工作目录返回
10、获得目录列表🔗
#include <dirent.h>
DIR * opendir(const char * pathname)
//打开目录文件
struct dirent * readdir(DIR * dir)
//读出目录文件内容
int closedir(DIR * dir)
//关闭目录文件
//读目录的代码
int main(int arg, char *args[])
{
if (arg <2)
return 0;
DIR *dp;
struct dirent *dirp;
dp = opendir(args[1]);//打开目录文件
if (dp == NULL)
{
printf("error is %s\n", strerror(errno));
return 0;
}
while((dirp = readdir(dp)) != NULL)//用readdir循环读取目录内容,读到目录尾,循环break
{
printf("%s\n", dirp->d_name);//将目录下的文件名打印到屏幕
}
closedir(dp);//关闭目录
return 0;
}
“`🔗
备份地址: 【Linux文件操作】