一、fseek
void clearerr(FILE *stream);清除一个流出错的标记
1.fseek函数
int fseek(FILE *stream, long offset, int whence);
功能:将stream流文件中的文件指针从whence位置开始
偏移offset字节的长度。
参数:stream 要移动文件指针的目标文件流对象。
注意:不支持设备文件,一般用于普通文件。
offset 要在文件内偏移的距离,单位字节。
如果值为整数,则向文件末尾偏移
如果值为负数,则向文件开头偏移
whence 偏移的起始位置,由系统定义的三个宏开始。
SEEK_SET 文件的开头位置
SEEK_CUR 文件的当前位置
SEEK_END 文件的末尾位置
返回值:
成功: 返回 0
失败: -1;
如果从文件的指定位置向后偏移过程中已经超过了文件的当前末尾位置,则会自动以'\0'来填充文件内容,从而形成一种被称为"空洞文件" 的特殊文件。
2.ftell函数
long ftell(FILE *stream);
功能:获取当前文件流指针的具体位置,一般以文件
开头到当前指针的字节数为返回值。
参数:stream 要返回指针距离的文件流对象
返回值:成功 获取到的距离长度,单位是字节
失败 -1;
练习:计算文件字节数
#include<stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen("2.txt","r");
if(NULL==fp)
{
printf("error\n");
return 1;
}
fseek(fp,0,SEEK_END);
long size=ftell(fp);
fclose(fp);
printf("size is %ld\n",size);
return 0;
}
rewind(fp);
rewind:主要作用是将文件内部的位置指针重置回文件的开头
void rewind(FILE *stream);
调用 rewind(stream)
后,stream
指向的文件的位置指针会被重置到文件的开头。
rewind() 等效于:fseek(stream,0L,SEEK_SET);
二、缓冲区
fflush(stdout)
是一个在 C 语言中用于刷新标准输出缓冲区(stdout
)的函数调用。stdout
是一个指向标准输出设备的文件指针,通常是屏幕或终端。当使用如 printf
这样的函数向标准输出写入数据时,这些数据首先被存储在输出缓冲区中,而不是立即发送到目标设备。这样做可以提高性能,因为减少了实际执行 I/O 操作的次数。
然而,在某些情况下,你可能希望立即看到输出,而不是等待缓冲区满或程序结束时才发送。这时,fflush(stdout)
就非常有用。它强制将 stdout
缓冲区中的所有数据写入到其对应的目标设备(通常是屏幕或终端),然后清空缓冲区。
stdin标准输入
stdout标准输出
stderr标准错误输出,一般用在程序出现错误时。打印错误信息
1.行缓冲
行缓冲区大小1k, terminal,主要用于人机交互stdout。行缓存多是关于终端的一些操作
刷新条件:
1.遇到\n刷新
2.缓存区满刷新,缓存区满(1024个字节)
3.程序正常结束刷新
4.fflush函数刷新 fflush(stdout);
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
//不满足任意条件
/* 1
printf("hello");
while(1)
sleep(1);
*/
//缓存区满刷新
/* 2
int i=0;
for(i=0;i<1025;i++)
{
//printf("a");
fputc('a',stdout);// stdin stdout stderr
}
while(1)
sleep(1);
*/
//程序结束刷新
/*3
printf("hello");
*/
//fflush刷新
printf("hello");
fflush(stdout);
while(1)
sleep(1);
return 0;
}
2.全缓冲
4k,主要用于文件的读写 ,对普通文件进行标准IO操作,建立的缓存一般为全缓存
刷新条件:
1.缓存区满刷新,缓存区满刷新,缓存区 4096
2.程序结束刷新,正常结束。
3.fflush来刷新 fflush(fp);
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// 不刷新
/* 1
printf("hello");
while(1)
sleep(1);
*/
//缓存区满刷新
/* 2
int i=0;
for(i=0;i<1025;i++)
{
//printf("a");
fputc('a',stdout);// stdin stdout stderr
}
while(1)
sleep(1);
*/
//程序结束刷新
/*3
printf("hello");
*/
//fflush刷新
printf("hello");
fflush(stdout);
while(1)
sleep(1);
return 0;
}
3.无缓冲
0k 主要用于出错处理信息的输出 stderr
不对数据缓存直接刷新
printf();==>>stdout
fprintf(strerr,"fopen error %s",filename);
fprintf可以指定输出
三、文件IO
1.文件io
操作系统为了方便用户使用系统功能而对外提供的一组系统函数。称之为 系统调用 其中有个 文件IO:一般都是对设备文件操作,当然也可以对普通文件进行操作。
一个基于Linux内核的没有缓存的IO机制
2.文件io特性:
1 没有缓存区
2 操作对象不在是流,而是文件描述符 FILE* int 0-1023
3文件描述符:
很小的非负的整数 int 0-1023
内核每打开一个文件就会获得一个文件 描述符
文件描述符从3开始,012被标准输入输出、错误占用。
四、函数
1.open
open("1.c",O_WRONLY|O_CREAT,0666 );
int open(const char *pathname, int flags,int mode);
功能:
获得一个文件描述符
参数:
pathname:文件名
flags:
O_RDONLY :只读
O_WRONLY:只写
O_RDWR:读写
O_CREAT, 创建文件
O_EXCL,需要和O_CREAT同时使用,表示新建的文件不能存在,成功,否则open就会失败
O_NOCTTY,不是终端设备
O_TRUNC文件内容清空
O_APPEND追加
O_ASYNC异步io,什么时候io不确定,
O_NONBLOCK非阻塞
返回值:
成功返回文件描述符
失败返回-1
open函数,如果是创建,就要有3个参,加0666,8进制数表示权限。
2.write
ssize_t write(int fd, const void *buf, size_t count);
功能:
通过文件描述符向文件中写一串数据
参数:
fd:文件描述符
buf:要写入文件的字符串的首地址
count:要写入字符的个数(buf的实际有效长度)
返回值:
成功返回实际写入的个数
失败返回-1
3.read
ssize_t read(int fd, void *buf, size_t count);
功能:
通过文件描述符读取文件中的数据
参数:
fd:文件描述符
buf:存放数据空间的首地址
count:要读到数据的个数
返回值:
成功返回读到数据的个数(可以作为write函数的参数,写入的字节个数)
失败返回-1
读到文件结尾返回0
write 函数, 参数要写入实际有效产长度。
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
int fd=open("3.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[100]="hello";
int ret=write(fd,buf,5);
if(-1==ret)
{
fprintf(stderr,"write error\n");
return 1;
}
close(fd);
return 0;
}
练习:复制文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
// cp 1 2 ./a.out 1 2
int main(int argc, char *argv[])
{
if(argc<3)
{
fprintf(stderr,"usage:./a.out srcfile dstfile\n");
return 1;
}
int src_fd = open(argv[1],O_RDONLY);
int dst_fd = open(argv[2],O_WRONLY| O_CREAT|O_TRUNC,0666);
if(-1 == src_fd ||-1 == dst_fd)
{
fprintf(stderr,"open error\n");
return 1;
}
char buf[512]={0};
while(1)
{
int rd_ret = read(src_fd,buf,sizeof(buf));
if(rd_ret<=0)
{
break; //判断条件要写在write之前;
}
write(dst_fd,buf,rd_ret); //传入字节不能写成sizeof(buf),复制出来的文件会比实际大
}
close(dst_fd);
close(src_fd);
return 0;
}
4.lseek
off_t lseek(int fd, off_t offset, int whence);
类似于fseek,传的是文件描述符
功能:定位光标的位置
参数:fd:文件描述符
offset:偏移量
正:向后偏移
负:向前偏移
零:不偏移
whence:
SEEK_SET
SEEK_CUR
SEEK_END
返回值:
成功返回偏移量
失败返回-1