IO的概念和标准IO函数

embedded/2025/3/5 1:12:53/

作业:

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

#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.计算文件的行数

#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;
}


http://www.ppmy.cn/embedded/170047.html

相关文章

大白话React第十二章深入地掌握 React 的高级特性

1. React 前沿 Hooks 深入使用 1.1 useId 白话解释&#xff1a;在 React 里&#xff0c;有时候我们需要给组件生成唯一的 ID&#xff0c;就像每个人都有唯一的身份证号。useId 这个 Hook 就是专门干这个事儿的&#xff0c;它能在客户端和服务端都生成唯一的 ID&#xff0c;避…

【面经】CPP经典面试手撕{LRUCache、字典树、布隆过滤器}

文章目录 LRUCache字典树布隆过滤器 LRUCache class LRUCache {using ListIt list<pair<int, int>>::iterator;list<pair<int, int>> _LRUlist;int _capacity;unordered_map<int, ListIt> _hashmap;public:LRUCache(int capacity) : _capacity(…

Vue项目性能优化、提取公共库(Common Chunks)

SplitChunksPlugin | webpack 中文文档 项目打包&#xff1a; npm run build 根目录下生成一个 dist 文件夹 css&#xff1a;当前项目中所有打包后的样式文件js&#xff1a;当前项目中所有打包后的 js 文件app.js 所有 src 目录下内容打包后的结果app.js.map&#xff1a;上面文…

Muduo + OpenSSL 网络交互完整流程

&#x1f525; Muduo OpenSSL 网络交互完整流程 这套架构结合了 Muduo&#xff08;网络库&#xff09; OpenSSL&#xff08;TLS/SSL 加密&#xff09; BIO&#xff08;缓存&#xff09;&#xff0c;整个数据流动过程如下&#xff1a; &#x1f30d; 1. 网络通信的基本流程 M…

Ubuntu中dpkg命令和apt命令的关系与区别

在 Ubuntu 中&#xff0c;dpkg 和 apt 是软件包管理的核心工具&#xff0c;但二者的角色和功能有显著区别&#xff1a; ​一、功能定位 ​特性​​**dpkg**​​**apt**​​层级​底层工具&#xff08;直接操作 .deb 文件&#xff09;高层工具&#xff08;管理软件仓库和依赖关…

HTML + CSS 题目

1.说说你对盒子模型的理解? 一、是什么 对一个文档进行布局的时候&#xff0c;浏览器渲染引擎会根据标准之一的css基础盒模型&#xff0c;将所有元素表示为一个个矩形的盒子。 一个盒子由四个部分组成: content&#xff0c;padding&#xff0c;border&#xff0c;margin 下…

Windows本地Docker+Open-WebUI部署DeepSeek

最近想在自己的电脑本地部署一下DeepSeek试试&#xff0c;由于不希望污染电脑的Windows环境&#xff0c;所以在wsl中安装了ollama&#xff0c;使用ollama拉取DeepSeek模型。然后在Windows中安装了Docker Desktop&#xff0c;在Docker中部署了Open-WebUI&#xff0c;最后再在Ope…

基于coze+微信小程序的ai对话

界面介绍&#xff1a; 代码&#xff1a;&#xff08;替换你的coze的配置&#xff09; <template><view class"container"><!-- 高斯模糊背景 --><view class"animated-bg"><view class"gradient-blob"></view…