IO的概念和标准IO函数

作业:

1.使用标准IO函数,实现文件的拷贝

复制代码
#include <stdio.h>

int main(int argc, char *argv[]) {
    // 检查是否提供了源文件和目标文件
    if (argc != 3) {
        printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
        return 1;
    }

    // 打开源文件以读取
    FILE *source = fopen(argv[1], "rb");
    if (source == NULL) {
        perror("Error opening source file");
        return 1;
    }

    // 打开目标文件以写入
    FILE *destination = fopen(argv[2], "wb");
    if (destination == NULL) {
        perror("Error opening destination file");
        fclose(source);  // 关闭源文件
        return 1;
    }

    // 逐块读取源文件并写入目标文件
    char buffer[1024];  // 缓冲区用于存储读取的数据
    size_t bytesRead;
    while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) {
        fwrite(buffer, 1, bytesRead, destination);  // 写入目标文件
    }

    // 检查读取和写入是否成功
    if (ferror(source)) {
        perror("Error reading from source file");
    }
    if (ferror(destination)) {
        perror("Error writing to destination file");
    }

    // 关闭源文件和目标文件
    fclose(source);
    fclose(destination);

    printf("File copied successfully.\n");

    return 0;
}

2.使用fgets函数,打印一个文件,类似cat

cs 复制代码
#include <stdio.h>

int main(int argc, char *argv[]) {
    // 检查是否传入了文件名参数
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        return 1;
    }

    // 打开文件
    FILE *file = fopen(argv[1], "r");
    if (file == NULL) {
        perror("Unable to open file");
        return 1;
    }

    char buffer[1024];  // 用于存储每行读取的内容
    // 逐行读取文件并打印
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        printf("%s", buffer);  // 打印当前行内容
    }

    // 关闭文件
    fclose(file);
    return 0;
}

3.计算文件的行数

cs 复制代码
#include <stdio.h>

int main(int argc, char *argv[]) {
    // 检查是否传入了文件名参数
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        return 1;
    }

    // 打开文件
    FILE *file = fopen(argv[1], "r");
    if (file == NULL) {
        perror("Unable to open file");
        return 1;
    }

    char buffer[1024];  // 用于存储每行读取的内容
    int lineCount = 0;  // 用于计数行数

    // 逐行读取文件并计数
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        lineCount++;  // 每读取一行,行数加1
    }

    // 打印文件的行数
    printf("The file has %d lines.\n", lineCount);

    // 关闭文件
    fclose(file);
    return 0;
}
相关推荐
AC赳赳老秦2 小时前
财报附注表格精准提取:OpenClaw 从 PDF 年报附注挖掘隐藏明细,补齐财务分析维度
java·汇编·c++·python·青少年编程·deepseek·openclaw
工具派3 小时前
Word文档太大怎么拆分?段落结构与两种拆法的边界实测
开发语言·c#·word
潜龙95273 小时前
需求文档一键变测试用例:手工用例智能生成平台
开发语言·c#
Lost of 程序猿3 小时前
命令模式实战:把“下单后的动作“打包成可执行的任务
后端·设计模式·c#·asp.net·命令模式
淡海水3 小时前
16-01-C#数据结构源码-附录A-术语表
unity·c#·游戏引擎·il2cpp
小羊没烦恼!4 小时前
【译】利用Asp.net MVC处理文件的上传下载
开发语言·缓存·c#·word·powerpoint·.net
波力海苔夹心脆6754 小时前
C# 海康威视摄像头二次开发入门:HCNetSDK 登录、实时预览、云台控制、录像与抓图(WinForms 实战)
开发语言·windows·经验分享·tcp/ip·c#
淡海水4 小时前
16-03-C#数据结构源码-附录C-参考资源
unity·c#·游戏引擎·il2cpp
asdzx675 小时前
Excel 水印方案对比:页眉图片 vs 背景图片(附 C# 实现)
c#·excel
影寂ldy5 小时前
C# TCP转串口Modbus网关终极完整版笔记(队列防抖+一问一答+帧解析+双客户端轮询)
笔记·tcp/ip·c#