linux 高级编程——文件IO

目录

feek函数:

ftell函数:

rewind函数:

缓冲:

①行缓冲

1k, terminal,主要用于人机交互stdout缓存区满或者遇到\n刷新 1024行缓存多是关于终端的一些操作1.遇到\n刷新2.缓存区满刷新3.程序结束刷新4.fflush刷新  fflush(stdout);②全缓冲

③无缓冲

文件描述符:

open函数;

write函数:

read函数:

lseek函数:


feek函数:

功能:

根据文件指针的位置和偏移量来定位文件指针

函数声明:int fseek(FILE *stream, long offset, int whence);

参数:

stream,文件的流(数据从文件当中流入或流出体现出来的字节叫流)

offset:相对于当前的偏移量

whence:文件指针定位的位置。SEEK_SET:指针在文件开头位置,SEEK_CUR:指针在当前位置

SEEK_END指针在文件最后一个字节的后一个。

假设file1文件内容为abcdef
使用fseek(file1,2,SEEK_SET);
偏移后指针指向c
再次使用fseek(file1,2,SEEK_CUR);
上一次指向c后,指针自动指向下一个,所以当前指针位置指向d,再偏移2后,指针指向f
fseek(file1,-1,SEEK_END);//此时指针指向最后一个字符

ftell函数:

函数声明:long ftell(FILE *stream)

功能:获取当前文件流指针的具体位置

返回值:一般以文件开头到指针指向位置的字节数的大小。

rewind函数:

功能:让文件指针回到起始位置

void rewind(FILE *stream);

缓冲:

①行缓冲

1k, terminal,主要用于人机交互stdout
缓存区满或者遇到\n刷新 1024
行缓存多是关于终端的一些操作
1.遇到\n刷新
2.缓存区满刷新
3.程序结束刷新
4.fflush刷新  fflush(stdout);

②全缓冲

4k,主要用于文件的读写
缓存区满刷新缓存区 4096
对普通文件进行标准IO操作,建立
的缓存一般为全缓存
刷新条件:
1.缓存区满刷新
2.程序结束刷新
3.fflush来刷新  fflush(fp);


③无缓冲

主要用于出错处理信息的输出 stderr 
不对数据缓存直接刷新
printf();==>>stdout 
fprintf(strerr,"fopen error %s",filename);
界面交互 出错处理
使用gdb查看,FILE结构体,或使用写入数据测试缓冲区。
缓冲区的大小是可以设置

文件描述符:

每个程序在启动的时候操作系统默认为其打开
  三个描述符与流对象匹配:
  0 ==>STDIN_FILENO === stdin
  1 ==>STDOUT_FILENO == stdout
  2 ==>STDERR_FILENO == stderr

open函数;

   函数声明:
   int open(const char *pathname, int flags);//文件已存在
   int open(const char *pathname, int flags, mode_t mode);//文件未存在

参数:

pathname :文件路径

flags:文件权限

w: O_WRONLY|O_CREAT|O_TRUNC

w+:O_RDWR|O_CREAT|TRUNC

r+:O_RDWR

a:O_WRONLY|O_CREAT|O_APPEND

a+:O_RDWR|O_CREAT_O_APPEND

write函数:

函数声明:ssize_t write(int fd, const void *buf, size_t count);

参数:

fd:文件描述符

const void *buf:要写的内容的地址(void *表示电影,照片,二进制文件都可以处理)

count:要写入文件的大小

返回值:成功返回写入文件的字节数,失败返回-1

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
    int fd = open("1.txt",O_WRONLY| O_CREAT|O_TRUNC,0666);    
    if(-1 == fd)
    {
        fprintf(stderr,"open error\n");
        return 1;
    }
    printf("fd is %d\n",fd);
    char buf[512]="hello";
    int ret = write(fd,buf,strlen(buf));
    if(-1 == ret)
    {
        fprintf(stderr,"write error\n");
        return 1;
    }

    close(fd);
    return 0;
}

read函数:

函数声明:ssize_t read(int fd, void *buf, size_t count);

参数:

fd:文件描述符

buf:读到的内容存放的首地址

count:一次读到的字节数

返回值:返回实际读到的字节数,返回0,表示到达结尾,返回-1表示失败

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

int main()
{
	int fb = open("8.txt",O_RDWR);
	if(fb == -1)
	{
		printf("open error");
		return 1;
	}
	else
	{
		printf("fb = %d\n",fb);
	}

	char buf[1024] = {0};

	while(1)
	{
		int ret = read(fb,buf,sizeof(buf));
		if(ret <= 0)
		{
			break;
		}

		printf("%s\n",buf);
	}

	close(fb);
}

lseek函数:

函数声明:off_t lseek(int fd, off_t offset, int whence);

参数:fd文件描述符

offset:相对whence的偏移量

whence:文件指针定位的位置。

SEEK_SET:指针在文件开头位置,SEEK_CUR:指针在当前位置

SEEK_END指针在文件最后一个字节的后一个。

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

int main()
{
	int fb = open("1.txt",O_RDWR);

	if(fb == -1)
	{
		printf("open error\n");
		return 1;
	}
	else
	{
		printf("fb = %d\n",fb);
	}
	
	char buf[] = "helloworldchina";
	off_t off = lseek(fb,10,SEEK_SET);

	write(fb,buf,strlen(buf));
	
	close(fb);
	return 0;
}

  • 16
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值