C语言的文件操作

目录

1、不带缓冲的I/O操作

1.1 creat函数(建立文件)

1.2 open函数(打开文件)

1.3 write函数(写入数据)

1.4 read函数(读取数据)

1.5 lseek函数(定位函数)

1.6 close函数(文件关闭函数)

1.7 综合案例:文件复制

 文件:cope_dome.c

2、带缓冲的I/O操作

2.1 3种缓冲类型

2.2 fopen函数

2.3 fclose函数

2.4 fdopen函数

2.5 fread函数

2.6 fwrite函数

2.7 fseek函数

2.8 ftell函数

2.9 fflush函数

2.10 setvbuf函数

3、 fgetc函数、getc函数、getchar函数

3.1 fgetc函数

3.2 getc函数

3.3 getchar函数

4、 fputc函数、putc函数、putchar函数

4.1 fputc函数

4.2 putc函数

4.3 putchar函数

5、 fgets函数与gets函数

5.1 fgets函数

5.2 gets函数

6、printf函数、fprintf函数、sprintf函数

6.1 printf函数

6.2 fprintf函数

6.3 sprintf函数

7、 scanf函数、fcanf函数、sscanf函数

7.1 scanf函数

7.1 fscanf函数

7.2 sscanf函数


文件描述符: 当某个程序打开文件时,操作系统返回相应的文件描述符,程序为了处理该文件必须引用此描述符。所谓的文件描述符,是一个低级的正整数。对于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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值