zoukankan      html  css  js  c++  java
  • 【UNIX程序设计教程】标准输入输出

    FILE * fopen(const char * pathname, const char * opentype);

    打开由pathname指定的文件并创建一个与之相连的流。

    int fclose(FILE * stream);

    打开的流通过fclose函数来关闭,在流被关闭前,所有缓冲区的输出将被写出,所有缓冲区的输入将被丢弃。

    FILE * freopen(const char * pathname, const char * opentype, FILE * stream);

    重新打开指定文件,并使之与一个指定的流(stream指定的流)相连。作用类似于fopen函数与fclose函数的合并。先关闭stream指定的流,按照opentype的方式打开pathname指定的文件,并使之与stream指定的流相连接。

    一旦打开一个流,就需要对流进行读写。有3种类型的无格式I/O函数:字符I/O函数,行I/O函数,块I/O函数。

    1.字符I/O函数

    字符输入函数中,getc函数最常用,其它两个基本不予使用。

    #include <stdio.h>
    int fgetc(FILE * stream);
    int getc(FILE * stream);
    int getchar(void);


    字符输出函数中,putc函数最常用。

    #include <stdio.h>
    int fputc(int c, FILE * stream);
    int putc(int c, FILE * stream);
    int putchar(int c);
     
    2.行I/O函数
    标准C库中有以下两个函数用于每次读入一行
    #include <stdio.h>
    
    char * fgets(char *s, int count, FILE * stream);
    char * gets(char * s);

    这里着重介绍一下fgets函数。

    作用:从stream指定的流中至多读入一行字符至参数s指定的字符串中,参数count指明字符串s的空间大小。该函数从流中连续读字符直至读到换行符或者读够count -1个字符为止。 所读入的这一行字符包含最后的换行符,存储在参数s指定的数组中,并且在其末尾加一个空字符(\0)作为该字符串结束标志。

    fgets函数比较安全,不会产生溢出,而gets函数没有为字符串s的溢出提供保护,因此我们最好不要使用gets。

    LINUX中的GNU C库专门提供了另外一个一次读入一行的函数getline,此外还扩充了一个更通用的函数getdelim。

    #include <stdio.h>
    
    ssize_t getline(char * * lineptr, size_t * n, FILE * stream);
    ssize_t getdelim(char * * lineptr, size_t * n, int delimter, FILE * stream);
  • 相关阅读:
    ubuntu安装pyton-pip问题解决
    Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused b
    git入门超详细(转载)
    openpose-opencv更改K分匹配算法实现
    年龄_性别识别
    人脸属性识别
    西门子PLC通过MODBUS控制变频器
    S7-200仿真软件使用
    lib/python3.6/site-packages/torchvision/_C.cpython-36m-x86_64-linux-gnu.so: undefined symbol:
    python_opencv修改视频分辨率
  • 原文地址:https://www.cnblogs.com/qi09/p/1820948.html
Copyright © 2011-2022 走看看