Linux Day2 文件IO(2)

标准IO的文件定位


        fseek

//函数原型
 
int feek(FILE * stream,long offset,int whence);
 
//参数:
//stream 要移动文件指针的目标文件流对象    注意:不支持设备文件,一般用于普通文件。
//offset 需要偏移的大小
//whence 从何处开始偏移    主要有以下的宏:
                            //SEEK_SET    文件开头
                            //SEEK_CUR    当前位置
                            //SEEK_END    文件结尾


        ftell

//函数原型
long ftell(FILE *stream);
 
//参数stream 文件流
 
//获取当前文件流指针的具体位置,一般以文件开头到当前指针的字节数为返回值


        rewind
 

/将文件流指针指向开头
 
void rewind(FILE * stream);
 
//可用fseek替代
fseek(fp,0,SEEK_SET);

     feof

功能:
      判断当前参数stream的文件流指针是否到达文件结尾。
      如果到达文件结尾则返回真,否则返回假
      注意:该操作一定要在一次IO操作之后判断。
参数:
      stream 要判断结尾的文件流对象
返回值:成功到达结尾是 真
        否则 是假


 

文件IO

概念:

什么是文件IO,又称系统IO,系统调用,是操作系统提供的API接口函数。

特性


.1 没有缓存区
.2 操作对象不在是流,而是文件描述符  FILE*    int 0-1023
.3文件描述符
很小的非负的整数 int   0-1023
内核每打开一个文件就会获得一个文件 描述符
  每个程序在启动的时候操作系统默认为其打开
  三个描述符与流对象匹配:
  0 ==>STDIN_FILENO === stdin
  1 ==>STDOUT_FILENO == stdout
  2 ==>STDERR_FILENO == stderr

标准I/O与文件I/O的区别

文件操作:
          缓存           操作对象                具体操作

标准IO    全缓存/行缓存   文件指针(流指针)FILE *   1.打开 --fopen  
                                                2.读写 
                                                  fgetc/fputc 
                                                  fgets/fputs 
                                                  fread/fwrite 
                                                3.关闭 
                                                  fclose 
                                                4.定位 
                                                  fseek/ftell/rewind 
                                                                                          
                                                  
文件IO    不带缓存        文件描述符 (整数)  int

                                                1.打开 --open 
                                                2.读写 --read/write
                                                3.关闭 --close 
                                                4.定位 --lseek 

缓冲区


       1. 行缓冲(大小为1k(1024个字节),主要用于人机交互)
               

刷新条件:

         1.遇到\n
         2.缓冲区满刷新
         3.程序结束
         4.fflush刷新至屏幕上         fflush(stdout);

        2. 全缓冲,大小为4k,主要用于文件的写操作 
               

 刷新条件:

       1.缓冲区满
       2.程序结束刷新
       3.fflush来刷新   //fflush(fp);

       3. 无缓冲,大小为0,主要用于出错处理信息的输出 (用stderr)不对数据缓存直接刷新

如:

 int fd=open("2.txt",O_RDWR);
 if(-1==fd)
 {
     fprintf(stderr,"open error\n");
     return 1;
 }

文件IO操作

open


1.open

int open(const char *pathname, int flags,int mode);


        功能:
            获得一个文件描述符
        参数:
           

            pathname:文件名
            flags:
            必须项:他们之间是互斥的,所以有且只能有一个 
                O_RDONLY
                O_WRONLY
                O_RDWR
            可选项:
            O_CREAT, 创建文件  //这个标志量存在,则需要指定参数 mode
            O_EXCL,  需要和O_CREAT同时使用,表示新建的文件不存在,成功,否则open就会失败
            O_TRUNC  文件内容清空
            O_APPEND追加
            
            //后面
            O_NOCTTY,不是终端设备
            O_ASYNC 异步io,什么时候io不确定,
            O_NONBLOCK 非阻塞 
               //0666是创建文件,0777是创建目录权限
                如:int fd = open("1.txt",O_WRONLY| O_CREAT|O_TRUNC,0666);
                  就是创建一个1.txt的文件

一般在创建文件时,都加入截断操作,标准io中w=O_CREAT | O_WRONLY | O_TRUNC

write


    2.write
       

char buf[1024];
        ssize_t write(int fd,  const  void *buf, size_t count);


        功能:
            通过文件描述符向文件中写一串数据
        参数:
         

  fd:文件描述符
  buf:要写入文件的字符串的首地址
  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


3.read
       

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


        功能:
            通过文件描述符读取文件中的数据
        参数:
           

 fd:文件描述符
 buf:存放数据空间的首地址
 count:要读到数据的个数


        返回值:
            成功返回读到数据的个数
            失败返回-1
            读到文件结尾返回0lssek()

举例:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    int fd = open("1.txt",O_RDONLY);    
    if(-1 == fd)
    {
        fprintf(stderr,"open error\n");
        return 1;
    }
    printf("fd is %d\n",fd);
    char buf[512]={0};
    while(1)
    {
        int ret = read(fd,buf,sizeof(buf));
        if(ret<=0)
        {
            break;
        }
        printf("%s\n",buf);
    }

    close(fd);
    return 0;
}

重点:用read和write完成对任意文件的复制
#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;
    }
    while(1)
    {
        char buf[512]={0};
        int rd_ret = read(src_fd,buf,sizeof(buf));
        if(rd_ret<=0)
        {
            break;
        }
        write(dst_fd,buf,rd_ret);
    }

    close(dst_fd);
    close(src_fd);
    return 0;
}


4.lseek  ==  fseek, rewind ftell
     

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


        功能:
            定位文件的位置
        参数:

    fd:文件描述符
            offset:偏移量
                        正:向后偏移
                        负:向前偏移
                        零:不偏移
            whence:
                SEEK_SET
                SEEK_CUR
                SEEK_END


        返回值:
            成功返回偏移量
            失败返回-1
            lseek(fd,0,SEEK_END);


作业:实现文件插入功能
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include<string.h>
int main(int argc, const char *argv[])
{
    int fd=open("2.txt",O_RDWR);
    if(-1==fd)
    {
        fprintf(stderr,"open error\n");
        return 1;
    }

    char buf[512]={0};
    char buf2[512]="mao12";
    lseek(fd,5,SEEK_SET);
    size_t  k=read(fd,buf,sizeof(buf));      
    lseek(fd,-k,SEEK_CUR);

    write(fd,buf2,strlen(buf2));
    write(fd,buf,k);

    close(fd);

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值