zoukankan      html  css  js  c++  java
  • 标准IO库函数复习

    打开文件,打开文件一定要成对写,养成好习惯很重要。比如 fopen()fclose
    <ol>
    <li>fopen()</li>

    <pre lang="c" escaped="true">
    /* fopen() */
    FILE *fopen(const char *path, const char *mode);

    FILE *fdopen(int fd, const char *mode);

    FILE *freopen(const char *path, const char *mode, FILE *stream);
    </pre>

    mode:

    <strong>r</strong> Open text file for reading. The stream is positioned at the beginning of the file.

    <strong>r+</strong> Open for reading and writing. The stream is positioned at the beginning of the file.

    <strong>w</strong> Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

    <strong>w+</strong> Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the
    file.

    <strong>a</strong> Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

    <strong>a+</strong> Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the
    beginning of the file, but output is always appended to the end of the file.

    &nbsp;
    &nbsp;

    <li>fclose()</li>

    <pre lang="c" escaped="true">
    /* fclose() */
    int fclose(FILE *stream);
    </pre>

    RETURN VALUE
    Upon successful completion 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case any further access (including
    another call to fclose()) to the stream results in undefined behavior.

    <li>EOF</li>

    <pre lang="c" escaped="true">
    /* End of file character.
    * some things throughout the library rely on this being -1. */
    #ifndef EOF
    #define EOF (-1)
    #endif

    /* stdin/stdout/stderr */
    stdin
    stdout
    stderr
    </pre>

    <li>errno</li>

    <pre lang="c" escaped="true">
    /* errno */
    void perror(const char *s);
    char *strerror(int errnum);

    int strerror_r(int errnum, char *buf, size_t buflen);
    /* XSI-compliant */

    char *strerror_r(int errnum, char *buf, size_t buflen);
    /* GNU-specific */

    char *strerror_l(int errnum, locale_t locale);

    fprintf(stderr, "filename:%s, line %d: %s. ", __FILE__, __LINE__, strerror(errno));
    </pre>


    <li>fgetc() fputc()</li>

    <pre lang="c" escaped="true">
    /* get char */
    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);

    /* put char */
    int fputc(int c, FILE *stream);

    int fputs(const char *s, FILE *stream);

    int putc(int c, FILE *stream);

    int putchar(int c);

    int puts(const char *s);
    </pre>


    举个例子如下:
    <pre lang="c" escaped="true">
    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    FILE *fp;
    int ch;

    if ((fp = fopen("test2", "w+")) == NULL)
    {
    perror("Open file test2. ");
    exit(1);
    }

    while ((ch = getchar()) != EOF)
    fputc(ch, fp);
    rewind(fp);
    while((ch = fgetc(fp)) != EOF)
    putchar(ch);

    fclose(fp);

    return 0;
    }
    </pre>

    运行结果:

    <blockquote>
    [sbso@localhost c]$ gcc fputc_fgetc_test.c -o fputc_fgetc_test
    [sbso@localhost c]$ ./fputc_fgetc_test
    hello world. www.emacsvi.com
    must enter ctrl-d over the input.
    ok goodbye.
    hello world. www.emacsvi.com
    must enter ctrl-d over the input.
    ok goodbye.
    [sbso@localhost c]$
    [sbso@localhost c]$ cat test2
    hello world. www.emacsvi.com
    must enter ctrl-d over the input.
    ok goodbye.
    [sbso@localhost c]$
    </blockqoute>

  • 相关阅读:
    超链接标签、链接地址、锚文本及图片标签
    有序无序列表,div盛放逻辑版块,table表格
    函数的默认值与动态参数arguments的总结
    浏览器中常见的html语义化标签
    html基本介绍,了解html与css,html语法和结构
    js函数与作用域,了解函数基本概念
    JavaScrip流程控制之switch选择,for循环
    JavaScript之if流程控制演练,if写在区间内怎么解决
    JavaScript数据类型typeof()和转换
    C++走向远洋——60(十四周阅读程序、STL中的简单容器和迭代器)
  • 原文地址:https://www.cnblogs.com/liweilijie/p/4984185.html
Copyright © 2011-2022 走看看