4. 利用标准I/O操作中的fopen、fread、fwrite、fseek、fclose等函数实现文件显示命令cat,读文件结束是用函数feof(fpr)返回文件指针为fpr的文件结束符,int feof(FILE* s)
时间: 2024-06-01 20:12:53 浏览: 29
#include <stdio.h>
int main(int argc, char *argv[]) {
if(argc != 2) { // 检查参数个数
printf("Usage: %s filename\n", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "r"); // 打开文件
if(fp == NULL) {
printf("Failed to open file %s\n", argv[1]);
return 1;
}
char buf[1024];
size_t nread;
while((nread = fread(buf, 1, sizeof(buf), fp)) > 0) { // 读取文件内容
fwrite(buf, 1, nread, stdout); // 输出到标准输出
}
if(feof(fp)) {
printf("End of file reached.\n");
} else if(ferror(fp)) {
printf("Error reading file %s\n", argv[1]);
}
fclose(fp); // 关闭文件
return 0;
}
相关问题
4. 利用标准I/O操作中的fopen、fread、fwrite、fseek、fclose等函数实现文件显示命令cat
以下是文件显示命令cat的实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
exit(1);
}
FILE *fp = fopen(argv[1], "r");
if (!fp) {
printf("Failed to open file: %s\n", argv[1]);
exit(1);
}
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buf = (char *)malloc(fsize + 1);
fread(buf, sizeof(char), fsize, fp);
buf[fsize] = '\0';
printf("%s", buf);
free(buf);
fclose(fp);
return 0;
}
```
该程序接受一个文件名作为命令行参数,打开该文件并读取其中的数据,最终将数据输出到标准输出中。程序使用了标准I/O操作中的fopen、fread、fwrite、fseek、fclose等函数。具体实现过程如下:
1. 首先判断命令行参数的数量是否正确,如果不正确则输出使用说明并退出程序。
2. 使用fopen函数打开文件,如果打开失败则输出错误信息并退出程序。
3. 使用fseek函数将文件指针移动到文件末尾,使用ftell函数获取文件大小,再使用fseek函数将文件指针移动回文件开头。
4. 动态分配一个大小为文件大小加一的字符数组作为缓冲区。
5. 使用fread函数将文件中的数据读取到缓冲区中,并在缓冲区末尾添加一个空字符。
6. 使用printf函数将缓冲区中的数据输出到标准输出中。
7. 释放缓冲区的内存并关闭文件。
4. 利用标准I/O操作中的fopen、fread、fwrite、fseek、fclose等函数实现文件显示命令cat和cp的功能。
1. cat命令实现:
```c++
#include <iostream>
#include <cstdio>
int main(int argc, char* argv[]) {
FILE* fp;
char buffer[1024];
if (argc < 2) {
std::cout << "Usage: cat <filename>" << std::endl;
return 1;
}
fp = fopen(argv[1], "r");
if (fp == nullptr) {
std::cout << "Error opening file" << std::endl;
return 1;
}
while (fgets(buffer, 1024, fp)) {
std::cout << buffer;
}
fclose(fp);
return 0;
}
```
2. cp命令实现:
```c++
#include <cstdio>
int main(int argc, char* argv[]) {
FILE* source;
FILE* destination;
char buffer[1024];
size_t count;
if (argc < 3) {
printf("Usage: cp <source> <destination>\n");
return 1;
}
source = fopen(argv[1], "rb");
if (source == nullptr) {
printf("Error opening source file\n");
return 1;
}
destination = fopen(argv[2], "wb");
if (destination == nullptr) {
printf("Error opening destination file\n");
fclose(source);
return 1;
}
while ((count = fread(buffer, 1, 1024, source))) {
fwrite(buffer, 1, count, destination);
}
fclose(source);
fclose(destination);
return 0;
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)