#创作计划#简单又实用的freopen
2025-07-31 22:23:09
发布于:浙江
因AAA混泥土批发ppl哥要求,这边也是讲解一下freopen
函数,(请输人文本
freopen
函数详解:简单实用的输入输出重定向指南
freopen
是 C/C++ 标准库中一个强大而实用的函数,用于重定向标准输入输出流。它在算法竞赛、文件处理和调试场景中非常有用。
参数详解:
filename
:文件路径(相对或绝对)mode
:文件打开模式"r"
:只读模式(文件必须存在)"w"
:写入模式(创建新文件或覆盖已有文件)"a"
:追加模式(在文件末尾添加内容)"r+"
:读写模式(文件必须存在)"w+"
:读写模式(创建新文件或覆盖已有文件)"a+"
:读写模式(在文件末尾添加内容)- 附加
b
表示二进制模式(如"rb"
,"wb"
)
stream
:要重定向的流stdin
:标准输入(通常来自键盘)stdout
:标准输出(通常显示在终端)stderr
:标准错误输出
返回值:
- 成功:返回指向流的指针
- 失败:返回
NULL
基本使用示例
1. 重定向输入(从文件读取)
#include <stdio.h>
int main() {
freopen("input.txt", "r", stdin); // 从文件读取输入
int num;
while(scanf("%d", &num) != EOF) {
printf("Read: %d\n", num);
}
fclose(stdin);
return 0;
}
2. 重定向输出(写入文件)
#include <stdio.h>
int main() {
freopen("output.txt", "w", stdout); // 写入文件
printf("This will be written to output.txt\n");
for(int i = 1; i <= 5; i++) {
printf("Line %d\n", i);
}
fclose(stdout);
return 0;
}
3. 同时重定向输入和输出
#include <stdio.h>
int main() {
freopen("input.txt", "r", stdin);
freopen("result.txt", "w", stdout);
int a, b;
scanf("%d %d", &a, &b);
printf("Sum: %d\n", a + b);
printf("Product: %d\n", a * b);
fclose(stdin);
fclose(stdout);
return 0;
}
高级用法技巧
1. 错误处理
#include <stdio.h>
#include <errno.h> // 用于错误号
#include <string.h> // 用于strerror
int main() {
if(freopen("nonexistent.txt", "r", stdin) == NULL) {
perror("Error opening file"); // 自动添加错误描述
printf("Error details: %s\n", strerror(errno));
return 1;
}
// 正常处理代码
return 0;
}
2. 临时重定向(保存原始流)
#include <stdio.h>
int main() {
// 保存原始标准输出
FILE *original_stdout = stdout;
// 重定向到文件
freopen("log.txt", "w", stdout);
printf("This goes to log file\n");
// 恢复原始标准输出
stdout = original_stdout;
printf("This goes back to console\n");
return 0;
}
3. 重定向标准错误
#include <stdio.h>
int main() {
freopen("errors.log", "w", stderr);
fprintf(stderr, "This is an error message\n");
perror("File operation failed");
fclose(stderr);
return 0;
}
4. 竞赛编程模板(自动切换本地/在线模式)
#include <stdio.h>
int main() {
#ifdef ONLINE_JUDGE
// 在线评测时使用标准输入输出
#else
// 本地调试时重定向到文件
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// 解题代码
int n;
scanf("%d", &n);
printf("%d\n", n * 2);
return 0;
}
常见问题解答
Q1: freopen
后需要使用 fclose
吗?
解: 程序结束时会自动关闭所有打开的文件流,但显式关闭是良好实践:
- 防止资源泄漏(长时间运行的程序)
- 确保缓冲区内容写入文件
- 避免达到系统文件打开上限
Q2: 如何同时处理文件和终端输出?
解: 使用 fprintf
指定输出目标:
FILE *log_file = fopen("log.txt", "w");
// 同时输出到终端和文件
printf("To console\n");
fprintf(log_file, "To file\n");
fclose(log_file);
Q3: 为什么 freopen
后 printf
不输出到控制台了?
解: 因为标准输出被重定向到文件了。如果需要同时输出到文件和终端:
#include <stdio.h>
int main() {
// 保存原始标准输出
FILE *console = stdout;
// 重定向到文件
freopen("output.txt", "w", stdout);
// 输出到文件
printf("This goes to file\n");
// 输出到控制台
fprintf(console, "This goes to console\n");
// 恢复标准输出(可选)
stdout = console;
printf("Back to console\n");
return 0;
}
Q4: 如何处理相对路径和绝对路径?
解:
- 相对路径:相对于程序运行目录
freopen("data/input.txt", "r", stdin);
- 绝对路径:指定完整路径
freopen("/home/user/project/data/input.txt", "r", stdin);
- 跨平台路径:使用预处理指令
#ifdef _WIN32 freopen("C:\\data\\input.txt", "r", stdin); #else freopen("/home/user/data/input.txt", "r", stdin); #endif
全部评论 3
把人写成入,把入写成人
14小时前 来自 浙江
0√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√
12小时前 来自 浙江
0
是自己写的吗?
昨天 来自 浙江
0bd
昨天 来自 浙江
0你是浙江哪里的
昨天 来自 浙江
0请输入文本
昨天 来自 浙江
0
昨天 来自 浙江
0逆如天
昨天 来自 浙江
0
有帮助,赞一个