标准IO操作基础函数基本使用

fopen()

  1. 说明
fopen, fdopen, freopen - stream open functions
#include <stdio.h>
FILE *fopen(const char *pathname, const char *mode);
  1. 例子1
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
int main()
{
    FILE *fp;

    fp=fopen("tmp","r+");
    if(fp==NULL)
    {
//      fprintf(stderr,"fopen() failed! error=%d\n",errno);
        //perror("fopen()");
        fprintf(stderr,"fopen():%s\n",strerror(errno));
        exit(1);
    }
    puts("OK!");

    fclose(fp);
    exit(0);
}

  1. 例子2 —测试系统可以打开的最大文件数量。
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
int main()
{
	int count=0;
	FILE *fp=NULL;

	while(1)
	{
		fp=fopen("tmp","r+");
		if(fp==NULL)
		{
			perror("fopen()");
			
			break;
		}
		count++;
	}
	printf("count=%d\n",count);
    
	exit(0);
}
   

fgetc()

  1. 说明
 int fgetc(FILE *stream);
 char *fgets(char *s, int size, FILE *stream);
 int getc(FILE *stream);int getchar(void);
 int ungetc(int c, FILE *stream);
  1. 例子
//测试文件中有多少有效字符
#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
    FILE *fp=NULL;
    int count=0;
    if(argc<2)
    {
        fprintf(stderr,"Usage %s<src_filename>",argv[0]);
        exit(1);
    }


    fp=fopen(argv[1],"r");


    if(fp==NULL)
    {
        perror("fopen()");
        exit(1);
    }
    while(fgetc(fp)!=EOF)
    {
        count++;
    
    }
    printf("the size of the file is %d Byte\n",count);
    fclose(fp);
    exit(0);
}

COPY命令的三种实现

  1. fgetc(),fputc()
#include<stdio.h>
#include<stdlib.h>

int  main(int argc,char*argv[])
{
    FILE *fps,*fpd;

    int ch;
    //输入参数要进行判断
    if(argc<3)
    {
        fprintf(stderr,"Usage:%s<src_filename><dst_filename>\n",argv[0]);
        exit(1);
    }

    fps=fopen(argv[1],"r");
    if(fps==NULL)
    {
        perror("fopen()");
        exit(1);
    }
    fpd=fopen(argv[2],"w");
    if(fpd==NULL)
    {
        fclose(fps);
        perror("fopen()");
        exit(1);
    }
    while(1)
    {
       ch= fgetc(fps);
       if(ch==EOF)
           break;
       fputc(ch,fpd);
    }
    fclose(fpd);
    fclose(fps);

    exit(0);
}
  1. fgets() ,fputs()
#include<stdio.h>
#include<stdlib.h>

#define BUFFSIZE 1024
int  main(int argc,char*argv[])
{
    FILE *fps,*fpd;
    char *buf[BUFFSIZE];
    //输入参数要进行判断
    if(argc<3)
    {
        fprintf(stderr,"Usage:%s<src_filename><dst_filename>\n",argv[0]);
        exit(1);
    }

    fps=fopen(argv[1],"r");
    if(fps==NULL)
    {
        perror("fopen()");
        exit(1);
    }
    fpd=fopen(argv[2],"w");
    if(fpd==NULL)
    {
        fclose(fps);
        perror("fopen()");
        exit(1);
    }
    while(fgets(buf,BUFFSIZE,fps)!=NULL)
    {
       fputs(buf,fpd);
    }
    fclose(fpd);
    fclose(fps);

    exit(0);
}
  1. fread(),fwrite()
#include<stdio.h>
#include<stdlib.h>
#define BUFFSIZE 1024
int  main(int argc,char*argv[])
{
    FILE *fps,*fpd;
    char *buf[BUFFSIZE]; 
    int n=0;
    int ch;
    //输入参数要进行判断
    if(argc<3)
    {
        fprintf(stderr,"Usage:%s<src_filename><dst_filename>\n",argv[0]);
        exit(1);
    }

    fps=fopen(argv[1],"r");
    if(fps==NULL)
    {
        perror("fopen()");
        exit(1);
    }
    fpd=fopen(argv[2],"w");
    if(fpd==NULL)
    {
        fclose(fps);
        perror("fopen()");
        exit(1);
    }
    while((n=fread(buf,1,BUFFSIZE,fps))>0)
    {
      //  printf("n=%d\n",n);
        fwrite(buf,1,n,fpd);
    }
    fclose(fpd);
    fclose(fps);

    exit(0);
}

fseek()

 #include <stdio.h>
 int fseek(FILE *stream, long offset, int whence);
 long ftell(FILE *stream);
 void rewind(FILE *stream);
 int fgetpos(FILE *stream, fpos_t *pos);
 int fsetpos(FILE *stream, const fpos_t *pos);

缓冲区

作用:大多数情况下是好处,合并系统调用
行缓冲:换行时候刷新,缓冲区满了的时候刷新,强制刷新
全缓冲:满了的时候刷新,强制刷新。
无缓冲:如stderr,需要立即输出的内容,无缓冲区

/*
代码示例
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
//1、强制刷新
    printf("while()前");
    fflush(stdout);
    while(1);
    fprintf(stderr,"while后");
 /*
 //2、换行时刷新
  printf("while()前\n");
    fflush(stdout);
    while(1);
    fprintf(stderr,"while后\n");
 */
  //3、无缓冲模式 stderr是无缓冲的
    fprintf(stderr,"while()前\n");
    while(1);
    fprintf(stderr,"while后\n");
 */
    exit(0);
}

空洞文件说明:一般下载工具在下载初始会先产生一个初始文件,而初始文件的大小就是最终下载完全的文件大小,避免后期因为磁盘空间不足导致下载失败。

getline()

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char *argv[])
{
    FILE *fp;
    char *linebuf;
    size_t linesize;
    if(argc<2)
    {
        fprintf(stderr,"Usage...\n");
        exit(1);
    }
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
        perror("fopen()");
        exit(1);
    }

    /*
     *初始化
     * */
    linebuf=NULL;
    linesize=0;

    while(1)
    {
        //成功则返回成功读取的字符数,包含分割符。
        //返回-1则失败
        if(getline(&linebuf,&linesize,fp)<0)
        {
            break;
        }
        printf("%ld\n",strlen(linebuf));
        printf("%ld\n",linesize);
    }
    fclose(fp);

    exit(0);
}

临时文件

场景:server端,用户提交的信息、请求等操作在服务器端有必要暂时保存,
1、如何不冲突的创建临时文件
2、需及时销毁

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值