C 标准库 - <stdio.h> 详解1

简介: C 标准库 - <stdio.h> 详解

在 C 语言中,stdio.h 是一个非常重要的头文件,定义了一系列用于输入和输出的函数、变量和宏。本文将逐一介绍 stdio.h 中定义的函数,并提供每个函数的完整示例。

变量类型

stdio.h 中定义了三个变量类型:

  1. size_t:无符号整数类型,通常用于表示内存大小。
  2. FILE:适合存储文件流信息的对象类型。
  3. fpos_t:适合存储文件中任何位置的对象类型。

宏定义

stdio.h 中定义了一些常用的宏:

  1. NULL:空指针常量的值。
  2. _IOFBF_IOLBF_IONBF:用于 setvbuf 函数的第三个参数。
  3. BUFSIZsetbuf 函数使用的缓冲区大小。
  4. EOF:表示文件结束的负整数。
  5. FOPEN_MAX:系统可以同时打开的文件数量。
  6. FILENAME_MAX:字符数组可以存储的文件名的最大长度。
  7. L_tmpnamtmpnam 函数创建的临时文件名的最大长度。
  8. SEEK_CURSEEK_ENDSEEK_SETfseek 函数中用于定位不同位置的宏。
  9. TMP_MAXtmpnam 函数可生成的独特文件名的最大数量。
  10. stderrstdinstdout:分别对应标准错误、标准输入和标准输出流。

函数介绍与示例

1. int fclose(FILE *stream)

关闭流 stream。刷新所有的缓冲区。

#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("test.txt", "w");
    fprintf(fp, "This is just a test.\n");
    fclose(fp);
    return 0;
}

2. void clearerr(FILE *stream)

清除给定流 stream 的文件结束和错误标识符。

#include <stdio.h>

int main() {
    FILE *fp;
    int c;

    fp = fopen("test.txt", "r");
    clearerr(fp);
    while ((c = fgetc(fp)) != EOF) {
        putchar(c);
    }
    fclose(fp);
    return 0;
}

3. int feof(FILE *stream)

测试给定流 stream 的文件结束标识符。

#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("test.txt", "r");
    while (!feof(fp)) {
        putchar(fgetc(fp));
    }
    fclose(fp);
    return 0;
}

4. int ferror(FILE *stream)

测试给定流 stream 的错误标识符。

#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("test.txt", "r");
    if (ferror(fp)) {
        perror("Error reading file");
    } else {
        printf("File read successfully.\n");
    }
    fclose(fp);
    return 0;
}

5. int fflush(FILE *stream)

刷新流 stream 的输出缓冲区。

#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("test.txt", "w");
    fprintf(fp, "This is just a test.\n");
    fflush(fp); // Flush the buffer to ensure data is written immediately
    fclose(fp);
    return 0;
}

6. int fgetpos(FILE *stream, fpos_t *pos)

获取流 stream 的当前文件位置,并把它写入到 pos

#include <stdio.h>

int main() {
    FILE *fp;
    fpos_t position;

    fp = fopen("test.txt", "r");
    fgetpos(fp, &position);
    printf("Current position in file: %lld\n", position);
    fclose(fp);
    return 0;
}

7. FILE *fopen(const char *filename, const char *mode)

使用给定的模式 mode 打开 filename 所指向的文件。

#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("test.txt", "w");
    if (fp == NULL) {
        perror("Error opening file");
        return -1;
    }
    fprintf(fp, "This is just a test.\n");
    fclose(fp);
    return 0;
}

8. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

从给定流 stream 读取数据到 ptr 所指向的数组中。

#include <stdio.h>

int main() {
    FILE *fp;
    char buffer[20];

    fp = fopen("test.txt", "r");
    fread(buffer, sizeof(char), 10, fp);
    printf("Data read: %s\n", buffer);
    fclose(fp);
    return 0;
}

9. FILE *freopen(const char *filename, const char *mode, FILE *stream)

把一个新的文件名 filename 与给定的打开的流 stream 关联,同时关闭流中的旧文件。

#include <stdio.h>

int main() {
    FILE *fp;
    fp = freopen("test.txt", "w", stdout);
    printf("This is redirected to test.txt\n");
    fclose(fp);
    return 0;
}

10. int fseek(FILE *stream, long int offset, int whence)

设置流 stream 的文件位置为给定的偏移 offset,参数 whence 意味着从给定的位置查找的字节数。

#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("test.txt", "r");
    fseek(fp, 5, SEEK_SET);
    printf("Character at position 5: %c\n", fgetc(fp));
    fclose(fp);
    return 0;
}

11. int fsetpos(FILE *stream, const fpos_t *pos)

设置给定流 stream 的文件位置为给定的位置。参数 pos 是由函数

fgetpos 给定的位置。

#include <stdio.h>

int main() {
    FILE *fp;
    fpos_t position;

    fp = fopen("test.txt", "r");
    fgetpos(fp, &position);
    fseek(fp, 10, SEEK_SET);
    fsetpos(fp, &position);
    printf("Character at original position after seeking: %c\n", fgetc(fp));
    fclose(fp);
    return 0;
}

12. long int ftell(FILE *stream)

返回给定流 stream 的当前文件位置。

#include <stdio.h>

int main() {
    FILE *fp;
    long int position;

    fp = fopen("test.txt", "r");
    fseek(fp, 0, SEEK_END);
    position = ftell(fp);
    printf("Size of file: %ld bytes\n", position);
    fclose(fp);
    return 0;
}

13. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

ptr 所指向的数组中的数据写入到给定流 stream 中。

#include <stdio.h>

int main() {
    FILE *fp;
    char buffer[20] = "Hello, World!";

    fp = fopen("test.txt", "w");
    fwrite(buffer, sizeof(char), 13, fp);
    fclose(fp);
    return 0;
}

14. int remove(const char *filename)

删除给定的文件名 filename,以便它不再被访问。

#include <stdio.h>

int main() {
    if (remove("test.txt") == 0) {
        printf("File deleted successfully.\n");
    } else {
        perror("Error deleting file");
    }
    return 0;
}

15. int rename(const char *old_filename, const char *new_filename)

old_filename 所指向的文件名改为 new_filename

#include <stdio.h>

int main() {
    if (rename("test.txt", "new_test.txt") == 0) {
        printf("File renamed successfully.\n");
    } else {
        perror("Error renaming file");
    }
    return 0;
}

16. void rewind(FILE *stream)

设置文件位置为给定流 stream 的文件的开头。

#include <stdio.h>

int main() {
    FILE *fp;
    char c;

    fp = fopen("test.txt", "r");
    rewind(fp);
    c = fgetc(fp);
    printf("First character of file after rewinding: %c\n", c);
    fclose(fp);
    return 0;
}

17. void setbuf(FILE *stream, char *buffer)

定义流 stream 应如何缓冲。

#include <stdio.h>

int main() {
    FILE *fp;
    char buffer[BUFSIZ];

    fp = fopen("test.txt", "w");
    setbuf(fp, buffer);
    fprintf(fp, "This is just a test.\n");
    fclose(fp);
    return 0;
}

18. int setvbuf(FILE *stream, char *buffer, int mode, size_t size)

另一个定义流 stream 应如何缓冲的函数。

#include <stdio.h>

int main() {
    FILE *fp;
    char buffer[BUFSIZ];

    fp = fopen("test.txt", "w");
    setvbuf(fp, buffer, _IOFBF, BUFSIZ);
    fprintf(fp, "This is just a test.\n");
    fclose(fp);
    return 0;
}

19. FILE *tmpfile(void)

以二进制更新模式(wb+)创建临时文件。

#include <stdio.h>

int main() {
    FILE *tmpfp;

    tmpfp = tmpfile();
    fprintf(tmpfp, "This is a temporary file.\n");
    fclose(tmpfp);
    return 0;
}

C 标准库 - <stdio.h> 详解2:https://developer.aliyun.com/article/1552425

目录
相关文章
|
2月前
|
存储 算法 C++
C++的常用标准库
C++的常用标准库docx
27 1
|
2月前
|
算法 程序员 C++
|
4天前
|
存储
C 标准库 - <stdio.h> 详解2
C 标准库 - <stdio.h> 详解
13 0
|
9天前
|
Java Unix 程序员
C 标准库
【6月更文挑战第22天】C 标准库。
14 3
|
4天前
|
程序员 C语言
C 标准库 - <ctype.h>
C 标准库 - <ctype.h>
4 0
|
4天前
|
存储 程序员
C 标准库 - <stdlib.h>
C 标准库 - <stdlib.h>
5 0
|
2月前
|
存储 自然语言处理 安全
C/C++ (stdio.h)标准库详解
C/C++ (stdio.h)标准库详解
170 0
|
编译器 C语言
#include <stdio.h> 这段代码是干什么的?底层原理是什么?为什么这样写?
#include <stdio.h> 这段代码是干什么的?底层原理是什么?为什么这样写?
179 0
|
Linux 索引 Perl