目录
6、printf函数、fprintf函数、sprintf函数
文件描述符: 当某个程序打开文件时,操作系统返回相应的文件描述符,程序为了处理该文件必须引用此描述符。所谓的文件描述符,是一个低级的正整数。对于Linux而言,所有设备和文件操作都使用文件描述符来进行。文件描述符是一个非负的整数,它是一个索引值,并指向内核中每个进程打开文件的记录表。当打开一个现存文件或创建一个新文件时,内核向进程返回一个文件描述符,当要读写文件时,也需要把文件描述符作为参数传递给相应的函数
1、不带缓冲的I/O操作
creat、open、read、write、lseek、close
1.1 creat函数(建立文件)
函数详解
表头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
定义函数
int creat(const char* pathname,mode_t mode);
函数说明
用于建立文件
参数pathname指向欲建立的文件路径的字符串
mode表示若文件名不存在,则给创建的文件赋权限(0755,从第二个开始分别表示文件所有者、文件用户组、其他用户),1.执行 2.写入 4.读
返回值
creat()会返回新的文件描述词
综合案例
/*通过文件名建立文件,文件权限为0755,该文件名为creat_dome.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//文件建立函数
void creat_file(char *filename)
{
if(creat(filename,0755) < 0) //若<0表示建立失败
{
printf("create file %s failure!\n",filename);
exit(-1);
}
else
{
printf("create file %s success!\n",filename);
}
}
//主函数
int main(int argc,char *argv[]) //argc存的的是参数个数,argv存的是参数名称
{
int i;
if(argc < 2) //先判断有几个参数
{
perror("you haven't input the filename,please try again!\n");
exit(-1);
}
for(i = 1;i < argc;i++)
{
creat_file(argv[i]);
}
return 0;
}
运行结果
linux@ubuntu:~/test$ gcc creat_dome.c -o creat_dome -Wall
linux@ubuntu:~/test$ ./creat_dome a.c b.c c.c
create file a.c success!
create file b.c success!
create file c.c success!
1.2 open函数(打开文件)
函数详解
表头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
定义函数
int open(const char* pathname,int flags);
int open(const char* pathname,int flags,mode_t mode);
函数说明
用于打开文件
参数pathname指向欲建立的文件路径的字符串
参数flags表示以何种方式打开文件
O_RDONLY:以只读的方式打开文件
O_WRONLY:以只写的方式打开文件
O_RDWR:以读写的方式打开文件
以上三种互斥,可与下列旗标利用OR(1)运算符进行组合。
O_CREAT:若欲打开的文件不存在则自动创建该文件
O_APPEND:以附加方式打开文件
O_SYNC:以同步方式打开文件
mode表示若文件名不存在,则给创建的文件赋权限(0755,从第二个开始分别表示文件所有者、文件用户组、其他用户),1.执行 2.写入 4.读
返回值
若所有欲核查的权限都通过了检查返回0值,表示成功,只要有一个权限被禁止则返回-1,若正确打开文件返回新的文件描述词
1.3 read函数
表头文件
#include <unistd.h>
定义函数
ssize_t read(int fd,void* buf,size_t count);
函数说明
从以打开的文件中读取数据
fd 文件描述词
buf 读取到的数据存储的地方
count 读取的字节数
返回值
返回值为实际读取到的字节数
综合案例
/*打开文件,然后去关闭文件,文件不存在则创建文件,该代码文件名为open_dome.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void open_file(char *filename);
int main(int argc,char *argv[])
{
int i = 1;
if(argc < 2)
{
perror("please input the open file pathname!\n");
exit(1);
}
for(i = 1;i < argc;i++)
{
open_file(argv[i]);
}
return 0;
}
void open_file(char *filename) //文件打开函数
{
int fd;
if((fd = open(filename,O_CREAT|O_RDWR,0755)) < 0)
{
perror("open file failure!\n");
exit(1);
}
else
{
printf("open file %s success!\n",filename);
}
close(fd);
printf("close file %s success!\n",filename);
return;
}
运行结果
/*a.c b.c c.c都存在,只打开,的d.c不存在,为它新建了*/
linux@ubuntu:~/test$ gcc open_dome.c -o open_dome -Wall
linux@ubuntu:~/test$ ./open_dome a.c b.c c.c d.c
open file a.c success!
close file a.c success!
open file b.c success!
close file b.c success!
open file c.c success!
close file c.c success!
open file d.c success!
close file d.c success!
1.3 write函数(写入数据)
函数详解
表头文件
#include <unistd.h>
定义函数
ssize_t write(int fd,const void *buf,size_t count);
函数说明
将数据写入已打开的文件内
fd 文件描述词
buf 待写入数据存储的地方
count 写入的字节数
返回值
返回值为实际写入的字节数
综合案例
/*打开文件a.c b.c c.c往文件中写入abc*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
void write_file(char* filename);
int main(int argc,char *argv[])
{
int i;
if(argc < 2)
{
perror("please input the open file pathname!\n");
exit(1);
}
for(i = 1;i < argc;i++)
{
write_file(argv[i]);
}
return 0;
}
void write_file(char* filename)
{
int fd;
char data[128];
sprintf(data,"this filename is %s",filename);
if((fd = open(filename,O_RDWR)) < 0)
{
printf("open file failure!\n");
return;
}
if(write(fd,data,strlen(data))<0)
{
perror("write file failure!\n");
}
else
{
printf("'%s' write success!\n",data);
}
close(fd);
return;
}
运行结果
linux@ubuntu:~/test$ gcc write_dome.c -o write_dome -Wall
linux@ubuntu:~/test$ ./write_dome a.c b.c c.c
'this filename is a.c' wri