Linux标准I/O(一)

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

/*************************************
**  #icnlude<stdio.h>
**  FILE *fopen(const char *pathname,const char *mode)
**  FILE *freopen(const char *pathname,const char *mode,FILE *fp)
**  FILE *fdopen(int fd,const char*mode)
**  说明:pathname是包含文件路径的文件名,mode是流文件的读写方式,fd是文件描述符
**
**  fopen打开一个指定的文件,并创建一个相连的流
**  freopen在一个特定的流上面打开一个特定的文件。它首先关闭fp指定的流,在打开由pathname指定的流
**  fdopen将一个流对应到某个一打开的文件
**
**  返回值:成功返回FILE 的指针,反之返回NULL
**  mode的取值有如下含义:r(读)  w(写) a(追加)b(二进制文件)

**  #include<stdio.h>
**  int fclose(FILE *fp);
**  int fcloseall();
**  fclose关闭指定的流指针,fcloseall是关闭所有的流
**  返回值:fclose成功返回0,反之返回非0
           fcloseall返回关闭的流的数量,失败返回EOF
*************************************/

int main(int argc,char *argv[])
{
    FILE * fp;
    int ret;

    if(argc<=1)
    {
        printf("argc error\n");
        return 1;
    }

    fp=fopen(*(argv+1),"r");
    if(fp==NULL)
    {
        printf("fopen error\n");
        return 2;
    }
    printf("%s is opened\n",argv[1]);

    ret=fclose(fp);
    if(ret!=0)
    {
        printf("fclose error\n");
        return 3;
    }
    return 0;
}

关于fclose和fcloseall的区别代码

#include<stdio.h>

int main(int argc,char * argv[])
{
    FILE *fp1,*fp2;

    if((fp1=fopen("file1.txt","w"))==NULL)
    {
        printf("fopen file1 error\n");
        return 1;
    }
    printf("file1 success\n");

    if((fp1=fopen("file2.txt","w"))==NULL)
    {
        printf("fopen file2 error\n");
        return 2;
    }
    printf("file2 success\n");

    if(fcloseall()==EOF)
    {
        printf("fcloseall error\n");
        return 3;
    }
    else
        printf("fcloseall success\n");
    return 0;
}
#include<stdio.h>

/*********************************
**  I/O的3种类型:
**    (1)字符I/O:每次读取/写入一个字符
**    (2)行I/O  :每次读/写一行字符
**    (3)块I/O  :每次读/写若干个对象
**
**  #include<stdio.h>
**  int fgetc(FILE *fp)  读取字符并转化为int类型存储
**  int getc(FILE *fp)
**  int getchar()
%%  int fputc(int c,FILE *fp) 将字符c转化为char,在写到fp中
%%  int putc(int c,FILE *fp)
%%  int putchar(int c)
*********************************/
int main(int argc,char *argv[])
{
    int word,line,chara;
    int c;
    int status;

    status=0;
    word=line=chara=0;
    while((c=getchar())!='0')
    {
        chara++;
        switch(c)
        {
            case '\n':line++;status=0;break;
            case ' ' :status=0;break;
            case '\t':status=0;break;
            default:
            if(status==0)
            {
                word++;
                status=1;
            }
            break;
        }
    }

    printf("there are %d characters,%d words,%d lines\n",chara,word,line);
    return 0;
}
#include<stdio.h>

/********************************
**  #include<stdio.h>
**  char * fgets(char *buf,int count,FILE fp)
**  char * gets(char *buf)
&&  int fputs(const char *str,FILE *fp)
&&  int puts(const char *str)
**  说明:buf是接受输入的缓冲区,count是需要接受的字符数,str是要输出的字符串
         前两个函数是输入,后两个是输出函数
**       读取文件时只能读取count-1个字符,自动添加\0
**       输出函数自动换行
**  返回值:输入函数如果读取成功返回指向buf的指针,如果失败返回NULL
********************************/

int main(int argc,char *argv[])
{
    char iobuf[1024];
    FILE *fp;
    if(argc<=1)
    {
        printf("argc error\n");
        return 1;
    }

    if((fp=fopen(argv[1],"r"))==NULL)
    {
        printf("fopen error\n");
        return 2;
    }
    while((fgets(iobuf,1024,fp))!=0)
    {
        puts(iobuf);
    }
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

/**************************
**   size_t fread(void *ptr,size_t size,size_t count,FILE *fp)
**   size_t fwrite(void *ptr,size_t size,size_t count,FILE *fp)
**   说明:ptr为指向若干个结构的指针,size为结构的大小,可以用sizeof求取
**        count是需要读取的结构的个数,fp是流的指针
**   fread()从fp指定的流中读取count个数据存放在ptr中,每个数据的大小是size,所以读取的总字节为size*count
**   fwrite()从ptr读取count个数据写入到fp指定的流中,每个数据的大小是size,所以写的从大小是size*count
**   返回值:返回读取/写入的数据项数,注意不是字节数
***************************/
#define namesize 30
struct
{
    char name[namesize];// 姓名
    long number; //学号
    short department;
    short scores[10];  //成绩
}student;

short *pscores;//保存学生成绩的数组

int main(int argc,char *argv[])
{
    FILE * fpStudent;
    FILE * fpPscores;

    if(argc<=2)
    {
        printf("argc error\n");
        return 1;
    }

    if((fpStudent=fopen(argv[1],"r"))==NULL)
    {
        printf("open file error\n");
        return 2;
    }

    if((fpPscores=fopen(argv[2],"w"))==NULL)
    {
        printf("write file error\n");
        return 3;
    }

    while((fread(&student,sizeof(student),1,fpStudent))==1)
    {
        pscores=student.scores;
        if((fwrite(&pscores,sizeof(pscores),3,fpPscores))!=3)
        {
            printf("fwrite error\n");
            return 4;
        }
    }
    return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/************************************
**  #include<stdio.h>
**  long int ftell(FILE *fp)
**  int fseek(FILE *fp,long int offset,int whence)
**  void rewind(FILE *fp)
**  NOtes: fp是文件流指针,offset是偏移量,whence是偏移起点--从什么地方开始
**  whence的取值有:SEEK_SET(文件定位于文件开始+offset处)
                   SEEK_CUR(文件定位于文件当前位置+offset处)
                   SEEK_END(文件定位于文件末尾+offset处)
**  返回值:fseek()成功返回0,反之返回非0
**  fseek()允许定位超出了文件当前末尾,如果在新位置写了数据,那么文件末尾便成了新位置,在新位置
**和原文件末尾之间的数据用0填充

**  rewind()定位于指定的fp文件的开头
**  rewind(fp)==fseek(fp,0L,SEEK_SET)

**  ftell()调用成功返回fp所指向的流的当前位置,它是从文件开始的字节数,否则返回-1
**  PS:重点关注fseek
************************************/

struct record
{
    int uid;
    char login[10];
};

char *login[]={"user1","user2","user3","user4","user5"};

//写出第i个位置上的记录
void Putec(FILE *fp,int i,struct record *ps)
{
    fseek(fp,(long int)sizeof(struct record)*i,SEEK_SET);
    fwrite((char *)ps,sizeof(struct record),1,fp);
}

int main(int argc,char *argv[])
{
    int i;
    FILE *fp;
    struct record rec;

    if(argc<=1)
    {
        printf("argc error\n");
        return 1;
    }

    if((fp=fopen(argv[1],"w"))==NULL)
    {
        printf("create file error\n");
        return 2;
    }

    for(i=4;i>=0;i--)
    {
        rec.uid=i;
        strcpy(rec.login,login[i]);
        Putec(fp,i,&rec);
    }
    fclose(fp);
    return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

/*****************************************
**  #include<stdio.h>
**  int fgetpos(FILE *fp,fpos_t *pos)
**  int fsetpos(FIle *fp,const fpos_t *pos)
**  说明:fp是流的指针,pos是指向fpos_t的指针,fpos_t是存放指针位置的记录类型
**  返回值:成功返回0,反之返回非0
**  注意:这两个函数可以在非UNIX的系统中使用,而fseek和ftell只能在类UNIX中使用
*****************************************/

char buf[132];
int main(int argc,char *argv[])
{
    FILE *fp;
    fpos_t pos;
    if(argc!=2)
    {
        printf("argc error\n");
        return 1;
    }

    if(argv[1][0]!='a')//打开一个文件
    {
        if((fp=fopen("tz.txt","w+"))==NULL)//打开问文件写数据
        {
            printf("w+ error\n");
            return 2;
        }
    }
    else//打开文件添加数据
    {
        if((fp=fopen("tz.txt","a+"))==NULL)
        {
            printf("a+ error\n");
            return 3;
        }
    }

    //写2行数据
    fputs("1234567890",fp);
    fputs("abcdefghij",fp);
    fseek(fp,0L,SEEK_END);
    fgetpos(fp,&pos);
    printf("current pos is %ld\n",pos);

    fseek(fp,30,SEEK_END);
    fgetpos(fp,&pos);
    printf("fseek(fp,30,SEEK_END) current pos is %ld\n",pos);

    fputs("abcdefg",fp);
    printf("write %c%s%c\n",'\"',"abcdefg",'\"');

    fgetpos(fp,&pos);
    printf("current pos is %ld\n",pos);
    fclose(fp);
    return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

/********************************
**  每个流对象内部都保持着两个指示器,一个是错误指示器,一个是文件结束指示器
**  当文件读写出错时,错误指示器被设置,当遇见文件尾时 文件结束指示器被设置
**  #include<stdio.h>
**  int ferror(FILE *fp)
**  int feof(FILE *fp)
**  void clearerr(FILE *fp)
**  说明:ferror是针对错误指示器的   feof是针对文件结束指示器的  clearerr清楚这两个标示
**  返回值:ferror返回1如果错误指示器被设置,否则返回0
**         feof返回非0如果文件结尾指示器被设置,否则返回0
********************************/

int main(int argc,char *argv[])
{
    int i;
    FILE *fp;

    if(argc<=1)
    {
        printf("argc error\n");
        return 1;
    }

    fp=fopen(argv[1],"w");
    fgetc(fp);
    printf("%d\n",ferror(fp));//由于文件为空,所以读取失败  返回1

    fputs("abcdefg",fp);
    fclose(fp);

    fp=fopen(argv[1],"r");
    fseek(fp,0,SEEK_END);
    fgetc(fp);
    if(feof(fp))printf("end of file\n");  //返回非0
    clearerr(fp);
    printf("%d %d\n",ferror(fp),feof(fp));//返回0 0
    fclose(fp);
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

/**********************************
**  读取或者写入数据都是以块为单位 流缓冲
**  目的:降低write和read的次数,因为他们都是系统调用,占很多的时间

**  流的三种类型:
**    (1)全缓冲 :缓冲区满了才发送或者读入数据。正常情况下,主存在磁盘上的文件是采用的全缓冲
**  所采用的缓冲区由I/O标准函数对第一次进行I/O操作时用malloc分配。
**    (2)行缓冲 :输入或者输出遇到换行符时才执行I/O操作,一般用于终端的交互
**    (3)无缓冲 :不设置缓冲区,从流中读出或者写入的字符单个单个的传出或者传入文件。标准错误流通常是
**  无缓冲的。

**  Linux中对于缓冲的默认值如下:
**  标准错误流是无缓冲的,其他的流用于交互设备的是行缓冲,其他的都是全缓冲

**  #include<stdio.h>
**  int setvbuf(FILE *fp,char *buf,int mode,size_t size);
**  void setbuf(FILE *fp,char *buf);
**  void setbuffer(FILE *fp,char *buf,size_t size);
**  void setlinebuf(FILE *fp);
**  说明:buf是自己设定的缓冲区,size是缓冲区的大小,mode是流的类型
**       mode的取值为_IOFBF 、_IOLBF 、_IONBF 分别表示全、行、无缓冲
**       如果设定无缓冲,那么buf和size就忽略,否则根据buf和size指定缓冲区和大小
**       如果buf设为NULL ,那么默认的size大小为BUFSIZE=256
**  返回值:setvbuf成功返回0,否则返回非0表示出错
**
**  setbuf是stuvbuf的特例,当buf为NULL时,setbuf(fp,NULL)==setvbuf(fp,NULL,_IONBF,BUFSIZE)
** 如果buf不为NULL,那么setbuf(fp,buf)==setvbuf(fp,buf,_IOFBF,BUFSIZE);

**  setlinebuf  设置行缓冲

**  #include<stdio.h>
**  int fflush(FILE *fp)
**  刷新缓冲区
**  返回值:成功返回0,反之返回EOF
**  注意:在读变写,或者写变成读的时候都要调用fflush
**********************************/

int get_line(char *buf,int bufsize,FILE *fp)
{
    if(fgets(buf,bufsize,fp)==NULL)
    {
        if(feof(fp)!=0)
        {
            printf("end of file\n");
            return EOF;
        }
        else
        {
            printf("fgets failed\n");
            return 0;
        }
    }
    printf("call:fgets():%s \n",buf);
    return 1;
}

char buf[33];
int main(int argc,char*argv[])
{
    FILE *fp;
    if(argc<=1)
    {
        printf("argc error\n");
        return 1;
    }

    if((fp=fopen(argv[1],"w"))==NULL)
    {
        printf("create file failed\n");
        return 2;
    }
    fprintf(fp,"this is the first line\n");
    fprintf(fp,"this is the secend line\n");
    fclose(fp);

    if((fp=fopen(argv[1],"r+"))==NULL)
    {
        printf("r+ file error\n");
        return 3;
    }
    get_line(buf,sizeof(buf),fp);
    fflush(fp);
    fprintf(fp,"this line should be new secend line\n");
    fflush(fp);
    get_line(buf,sizeof(buf),fp);
    fclose(fp);
    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值