第二章 C语言标准库<stdio.h>头文件用法汇总

3.1.1、标准输入输出函数

3.1.1.1、printffprintf
  • 功能:向标准输出或文件输出格式化字符串。
  • 示例:(需要在执行文件同级目录下新建一个output.txt的文件)
#include <stdio.h>

int main()
{
    int a = 10;
    float b = 3.14;
    printf("数值为:%d,浮点数为:%.2f\n", a, b); // 输出到屏幕

    FILE *file = fopen("output.txt", "w");
    fprintf(file, "文件输出:数值:%d,浮点数:%.2f\n", a, b); // 输出文件
    fclose(file);

    return 0;
}
3.1.1.2、scanffscanf
  • 功能:从标准输入或文件中读取格式化数据。
  • 示例:(注意:先写入数据,然后在读取数据,避免手动输入造成错误)
#include <stdio.h>

int main()
{
    int a;
    float b;
    printf("请输入一个数值和浮点数:\n");
    scanf("%d %f", &a, &b); // 从用户输入中读取数据
    printf("输入的数组是:%d.浮点数是:%.2f\n", a, b);

    FILE *file_w = fopen("intput.txt", "w");
    fprintf(file_w, "文件输出:数值:%d,浮点数:%.2f\n", a, b); // 输出文件
    fclose(file_w);

    FILE *file = fopen("input.txt", "r");
    fscanf(file, "%d %f", &a, &b); // 从文件中读取数据
    printf("文件中的数组是:%d.浮点数是:%.2f\n", a, b);
    fclose(file);
    return 0;
}
3.1.1.3、putsgets
  • 功能:处理整行字符串的输入输出。
  • 注意gets 在C11中已被弃用,推荐使用 fgets
  • 示例:(sizeof计算出申请的字符串长度,此例中输出长度为100)
#include <stdio.h>

int main()
{
    char str[100];
    printf("请输入一个字符串:\n");
    fgets(str, sizeof(str), stdin); // 安全从用户输入中读取字符串
    puts("输入的字符串是:");
    puts(str);
    return 0;
}
 

3.1.2、文件操作函数

3.1.2.1、fopenfclose
  • 功能:打开或关闭文件。
  • 示例
#include <stdio.h>

int main()
{
    FILE *file = fopen("input.txt", "w"); // 打开文件(w代表写入值)
    if (file == NULL)
    {
        printf("文件打开失败\n");
        return 1;
    }
    fprintf(file, "文件输出:数值:%d,浮点数:%.2f\n", 10, 3.14); // 输出文件
    fclose(file);

    int a[100];
    FILE *file_r = fopen("input.txt", "r"); // 打开文件(r代表读取值)
    fscanf(file_r, "%s", &a);               // 从文件中读取数据
    printf("文件中的数据是:%s.\n", a);
    fclose(file_r);
    return 0;
}

3.1.2.2、freadfwrite

  • 功能:二进制文件的读取与写入。
  • 示例
#include <stdio.h>

struct Data
{
    int id;
    char name[20];
};

int main()
{
    struct Data d1 = {1, "Alice"}, d2;
    FILE *file = fopen("input.bin", "wb");     // 打开文件(wb代表写入二进制值)
    fwrite(&d1, sizeof(struct Data), 1, file); // 输出文件
    fclose(file);

    file = fopen("input.bin", "rb");          // 打开文件(rb代表读取二进制值)
    fread(&d2, sizeof(struct Data), 1, file); // 从文件中读取数据
    printf("文件中的数据是:ID = %d, Name = %s.\n", d2.id, d2.name);
    fclose(file);
    return 0;
}
3.1.2.3、fseekftell
  • 功能:文件指针的移动和获取。
  • 示例
#include <stdio.h>

int main()
{
    FILE *file = fopen("input.txt", "w+");
    fputs("Hello, World!", file);

    fseek(file, 7, SEEK_SET); // 将文件指针移动到文件开头的第7个字节
    fputs("C语言程序测试", file);

    fseek(file, 0, SEEK_END); // 将文件指针移动到文件末尾
    long size = ftell(file);  // 获取文件大小
    printf("文件大小:%ld字节\n", size);

    fclose(file);
    return 0;
}

3.1.3、字符操作函数

3.1.3.1、fgetcfputc
  • 功能:处理单个字符的读取与写入。
  • 示例
#include <stdio.h>

int main()
{
    FILE *file = fopen("input.txt", "w+");
    fputc('A', file); // 写入字符
    fputc('B', file);
    rewind(file); // 重置文件指针
    printf("文件中的字符是:");
    printf("%c", fgetc(file)); // 读取字符
    printf("%c\n", fgetc(file));
    fclose(file);
    return 0;
}
3.1.3.2、getcputc
  • 功能:类似于 fgetcfputc,但更通用。
  • 示例
#include <stdio.h>

int main()
{
    FILE *file = fopen("input.txt", "w+");
    putc('X', file);
    putc('Y', file);
    rewind(file); // 重置文件指针
    printf("文件中的字符是:%c%c\n", getc(file), getc(file));
    fclose(file);
    return 0;
}

3.1.4、错误处理函数

3.1.4.1、perror
  • 功能:输出最后一次错误信息。
  • 示例
#include <stdio.h>
#include <errno.h>

int main()
{
    FILE *file = fopen("input.txt", "r");
    if (file == NULL)
    {
        perror("打开文件时发生错误");
        return 1;
    }
    return 0;
}
3.1.4.2、clearerrfeof
  • 功能:清除文件流错误标志和检测文件结束。
  • 示例
#include <stdio.h>

int main()
{
    FILE *file = fopen("input.txt", "r");
    if (file == NULL)
    {
        perror("打开文件时发生错误");
        return 1;
    }
    while (!feof(file))
    {
        char c = fgetc(file);
        if (ferror(file))
        {
            perror("读取文件时发生错误");
            break;
        }
        putchar(c);
    }
    clearerr(file); // 清除错误状态
    fclose(file);
    return 0;
}

3.1.5、临时文件和缓冲

3.1.5.1、tmpfiletmpnam
  • 功能:创建临时文件。
  • 示例
#include <stdio.h>

int main()
{
    FILE *file = tmpfile();
    if (file == NULL)
    {
        perror("创建临时文件时发生错误");
        return 1;
    }
    fprintf(file, "临时文件\n");
    rewind(file);
    char name[20];
    fgets(name, sizeof(name), file);
    printf("文件内容:%s\n", name);
    fclose(file);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值