Linux高级编程 8.13 标准IO(补充函数)

目录

1.fseek

2.ftell

3.rewind

4.缓冲区介绍


1.fseek

int fseek(FILE *stream, long offset, int whence);
功能:将stream流文件中的文件指针从whence位置开始
      偏移offset字节的长度。
参数:stream  要移动文件指针的目标文件流对象。
      注意:不支持设备文件,一般用于普通文件。
      offset  要在文件内偏移的距离,单位字节。
                如果值为整数,则向文件末尾偏移
              如果值为负数,则向文件开头偏移
      whence  偏移的起始位置,由系统定义的三个宏开始。
          SEEK_SET  文件的开头位置 
          SEEK_CUR  文件的当前位置
          SEEK_END  文件的末尾位置

返回值:    
    成功: 返回 0
    失败:  -1;

#include <stdio.h>

int main(int argc, const char *argv[])
{
	FILE *fp=fopen("dairy.txt","r");
	if(fp==NULL)
	{
		printf("fopen error\n");
	}

	int ret=fseek(fp,10,SEEK_SET);
	if(ret==-1)
	{
		printf("fseek error\n");
	}

	char buf[512]={0};
	fgets(buf,sizeof(buf),fp);
	printf("buf: %s\n",buf);

	fclose(fp);
	
	return 0;
}

2.ftell

long ftell(FILE *stream);rewind(fp);
功能:获取当前文件流指针的具体位置,一般以文件
     开头到当前指针的字节数为返回值。
参数:stream 要返回指针距离的文件流对象
返回值:成功 获取到的距离长度,单位是字节
        失败 -1;

#include <stdio.h>

int main(int argc, const char *argv[])
{
	FILE *fp=fopen("dairy.txt","r");
	if(fp==NULL)
	{
		printf("fopen error\n");
	}

	fseek(fp,0,SEEK_END);
	long size=ftell(fp);

	fclose(fp);
	printf("size: %ld\n",size);
	
	return 0;
}

通过fseek跳到文件最后的位置,在使用ftell可以求出文件大小

3.rewind

rewind()  等效于:fseek(stream,0L,SEEK_SET); 复位文件流指针

#include <stdio.h>

int main(void)
{
	FILE * fp=fopen("dairy.txt","r");
	if(fp==NULL)
	{
		printf("fopen error\n");
	}

	fseek(fp,0,SEEK_END);
	long size=ftell(fp);
	printf("size %ld\n",size);
	rewind(fp);
	char buf[512]={0};
	if(fgets(buf,sizeof(buf),fp))
	{
		printf("buf: %s\n",buf);
	}
	else
	{
		printf("end of file\n");
	}

	fclose(fp);
	return 0;
}

4.缓冲区介绍

stdin,相当于输入 
stdout,相当于输出,直接输出到屏幕
stderr,标准错误

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

#include <stdio.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
	/*1
	printf("hello");
	while(1)
		sleep(1);
*/
	/*2
	int i=0;
	for(i=0;i<1025;++i)
	{
		fputc('a',stdout);
	}

	while(1)
		sleep(1);
*/

	printf("hello");
	fflush(stdout);
	while(1)
		sleep(1);

	return 0;
}

通过while(1){sleep(1);阻塞程序
                
全缓冲,4k
    缓存区满刷新缓存区 4096
            多是对普通文件进行标准IO操作,文件读写的缓冲一般为全缓冲
            刷新条件:
                1.缓存区满刷新
                2.程序结束刷新
                3.fflush来刷新  fflush(fp);

#include <stdio.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
	FILE *fp=fopen("fullbuf.txt","w");
	if(fp==NULL)
	{
		return 1;
	}

	/*阻塞程序,不会输入任何东西
	char buf[]="hello";
	fputs(buf,fp);

	while(1)
		sleep(1);	
	*/

	/*1
	int i=0;
	for(i=0;i<4097;++i)
	{
		fputc('a',fp);
	}
	while(1)
		sleep(1);
	return 0;
	*/

	/*2
	char buf[]="hello";
	fputs(buf,fp);
	return 0;
	*/

	char buf[]="world";
	fputs(buf,fp);
	fflush(fp);

	while(1)
		sleep(1);

	return 0;
}

无缓冲,0k  主要用于出错处理信息的输出 stderr 
    不对数据缓存直接刷新
    printf();==>直接打印到屏幕上
    fprintf(stderr,"fopen error %s",filename);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值