文件IO(概念、打开、读、写、关闭)

一、文件IO的概念:

什么是文件IO,又称系统IO,系统调用

是操作系统提供的API接口函数。

POSIX接口 (了解)可移植操作系统接口

注意:文件IO不提供缓冲机制

文件IO的API

open  close read write

二、文件描述符概念:

英文:缩写fd(file descriptor)

文件描述符是一个非负整数。

是0-1023的数字,表示文件。

0, 1, 2 的含义 标准输入,标准输出,错误

三、文件IO 打开

open函数用来创建或打开一个文件:

#include <fcntl.h>

int open(const char *pathname, int flags);   不创建文件

pathname路径名字

flags标志

int open(const char *pathname, int flags, mode_t mode);  创建文件,不能创建设备文件

成功时返回文件描述符;出错时返回EOF

打开文件时使用两个参数

创建文件时第三个参数指定新文件的权限,(只有在建立新文件时有效)此外真正建文件时的权限会受到umask 值影响,实际权限是mode-umaks

可以打开设备文件,但是不能创建设备文件(创建设备mknode 驱动部分会讲)

文件IO和标准的模式对应关系:

r                                O_RDONLY//只读方式

r+                              O_RDWR//读写方式

w                               O_WRONLY | O_CREAT | O_TRUNC, 0664//以只写方式打开文件,若文件存在则文件长度清为0。若文件不存在则创建。

w+                             O_RDWR | O_CREAT | O_TRUNC, 0664

a                                O_WRONLY | O_CREAT | O_APPEND, 0664//以只写方式打开文件,若文件不存在则创建;若文件存在,向文件写入的数据被追加到文件末尾。

a+                              O_RDWR | O_CREAT | O_APPEND, 0664

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(){
	int fd;
	int ret;
	fd = open("test.txt",O_WRONLY|O_CREAT|O_TRUNC, 0666);//只写方式打开文件
	if(fd<0){
		printf("open file err\n");
		return  0;
	}
	printf("sucess,fe=%d\n",fd);

	ret = close(fd);
	if(ret<0){
		printf("close fail\n");
	}

	ret = close(fd);
	printf("ret=%d\n",ret);
}

四、umask概念:

头文件:

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

umask 用来设定文件或目录的初始权限

文件或目录的初始权限 = 文件或目录的最大默认权限 - umask权限

五、文件的关闭

头文件:#include <unistd.h>

int close(int fd)

成功返回0;出错时返回EOF

程序结束时自动关闭所有打开的文件

关闭后文件,描述符不能代表文件

六、文件IO的读写和定位

read函数用来从文件中读取数据:

#include  <unistd.h>

ssize_t  read(int fd, void *buf, size_t count);

成功时返回实际读取的字节数;出错时返回EOF

读到文件末尾时返回0

buf是接收数据的缓冲区

count不应超过buf大小

write函数用来向文件写入数据:

#include  <unistd.h>

ssize_t  write(int fd, void *buf, size_t count);

成功时返回实际写入的字节数;出错时返回EOF

buf是发送数据的缓冲区

count不应超过buf大小

lseek函数用来定位文件:

#include  <unistd.h>

off_t  lseek(int fd, off_t offset, intt whence);

成功时返回当前的文件读写位置;出错时返回EOF

参数offset和参数whence同fseek完全一样

whence参数:SEEK_SET/SEEK_CUR/SEEK_END

SEEK_SET 从距文件开头 offset 位移量为新的读写位置

SEEK_CUR:以目前的读写位置往后增加 offset 个位移量

SEEK_END:将读写位置指向文件尾后再增加 offset 个位移量

offset参数:偏移量,可正可负

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
	int fd;
	int ret;
	char buf[32] = "hello world";
	char buf2[32]={0};

	fd = open("test.txt",O_RDWR|O_CREAT|O_APPEND, 0666);//只读写方式打开文件
	if(fd<0){
		printf("open file err\n");
		return  0;
	}
	printf("sucess,fe=%d\n",fd);

	ret = write(fd,buf,strlen(buf));//写字符串到缓冲区,strlen计算字符串的长度,遇见\0结束,sizeof数组的长度
	if(ret<0){
		perror("write");
			goto END;
	}
	printf("write count=%d\n",ret);
/*
	close(fd);//重新关闭文件并打开,让指针回到最前面
	fd = open("test.txt",O_RDWR | O_CREAT | O_APPEND, 0666);
	if(fd<0){
		printf("open file err\n");
		return 0;
	}
*/
	lseek(fd,0,SEEK_SET);//定位文件到开头
	ret = read(fd,buf2,32);
	if(ret<0){
		perror("read");
		goto END;
	}
	
	printf("read buf2=%s\n",buf2);

END:
	close(fd);

}

容易出错点:

求字符串长度使用sizeof,对二进制数据使用strlen

printf 的字符最后没有’\0’

任务:使用文件IO实现“每隔1秒向文件1.txt写入当前系统时间,行号递增”

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

int main(){
	int fd;
	time_t ctime;
	int linecount = 0;
	struct tm *ctimestr;

	int ret;
	char buf[32];

	fd = open("test.txt",O_RDWR|O_CREAT|O_APPEND, 0666);//只读写方式打开文件
	if(fd<0){
		printf("open file err\n");
		return  0;
	}
	printf("sucess,fe=%d\n",fd);

	while(read(fd,buf,32) < 0){//获取文件中的行号
		if(buf[strlen(buf)-1] == '\n'){
			linecount++;
		}
	}
	while(1){
		ctime = time(NULL);//获取系统时间
		ctimestr = localtime(&ctime);//将系统时间转换为本地时间
		printf("%04d-%02d-%02d %02d:%02d:%02d\n",ctimestr->tm_year+1900,ctimestr->tm_mon+1,ctimestr->tm_mday,
											ctimestr->tm_hour,ctimestr->tm_min,ctimestr->tm_sec);//打印输出æ¶间
		sprintf(buf,"%d,%04d-%02d-%02d %02d:%02d:%02d\n",linecount,ctimestr->tm_year+1900,ctimestr->tm_mon+1,ctimestr->tm_mday,
											ctimestr->tm_hour,ctimestr->tm_min,ctimestr->tm_sec);//将时间输出到文件里
		
	ret = write(fd,buf,strlen(buf));//写字符串到缓冲区,strlen计算字符串的长度,遇见\0结束,sizeof数组的长度
	if(ret<0){
		perror("write");
			goto END;
	}
	
		linecount++;
		sleep(1);//休眠1秒
	}
END:
	close(fd);
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值