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;
}
相关推荐
武藤一雄1 小时前
WPF深度解析Behavior
windows·c#·.net·wpf·.netcore
蓝天星空1 小时前
C#中for循环和foreach循环的区别
开发语言·c#
LittleFishC2 小时前
08_长调用与短调用
c语言·汇编·逆向·windows内核
Maybe_ch2 小时前
WPF的STA线程模型、APM与TAP:从线程约束到现代异步
c#·.net·wpf
我是唐青枫3 小时前
C#.NET Consul + Steeltoe 深入解析:服务注册发现、健康检查与微服务接入
c#·.net·consul
波波0073 小时前
用微软AutoGen+ 通义千问实现 AI 成语接龙
人工智能·microsoft·c#
老师用之于民3 小时前
【DAY35】ARM开发:UART 异步串行通信原理、通信标准及模块配置详解
c语言·汇编·arm开发·vscode
csdn_aspnet14 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
武藤一雄18 小时前
C# 设计模式大全(第一弹|7种)
microsoft·设计模式·微软·c#·.net·.netcore
格林威19 小时前
Baumer相机锂电池极片裁切毛刺检测:防止内部短路的 5 个核心方法,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·视觉检测