C++简易缓冲区代码中exit(3)与exit(4)的错误语句定义及触发验证方案咨询
Fixing exit(3) and exit(4) Error Messages & Triggering Scenarios
First, let's correct your error messages and understand why your initial exit(4) scenario wasn't working:
Corrected Code
Your original code had incorrect messages for exit(3) and exit(4), plus a missing cleanup step (closing files on normal exit). Here's the revised version with proper error messages (using your initial Chinese ideas where applicable, adjusted for accuracy):
#include <fcntl.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <iostream> using namespace std; #define BUF_SIZE 500 #define OUTPUT_MODE 0700 int main(int argc, char *argv[]) { int in_file, out_file; int read_size = 1, write_size; char buf[BUF_SIZE]; if (argc != 3){ cout<<"命令行参数数量不正确,需传入源文件和目标文件两个参数"<<endl; exit(1); } in_file = open(argv[1], O_RDONLY); if (in_file < 0) { cout<<"无法打开输入文件"<<endl; exit(2); } out_file = creat(argv[2], OUTPUT_MODE); if (out_file < 0) { cout<<"无法创建输出文件"<<endl; // Correct exit(3) message exit(3); } while (read_size > 0) { read_size = read(in_file, buf, BUF_SIZE); if (read_size < 0){ cout<<"读取输入文件时出错"<<endl; // Corrected exit(4) message (empty file doesn't trigger this) exit(4); } write_size = write(out_file, buf, read_size); if (write_size <= 0){ close(in_file); close(out_file); cout<<"文件读写过程中出错"<<endl; exit(5); } } // Clean up: close files on successful completion close(in_file); close(out_file); cout<<"文件复制完成"<<endl; return 0; }
Key Fixes:
- exit(3) Message: Changed to "无法创建输出文件" as you intended, which matches the scenario where
creat()fails to create the output file. - exit(4) Message: Adjusted to "读取输入文件时出错" because your original idea ("待读取的文件为空") doesn't trigger this exit code.
read()returns 0 for empty files (end-of-file), which is a normal condition, not an error. Exit(4) is for actual read failures (e.g., permission loss mid-read, filesystem errors). - Added File Cleanup: Closed both files when the program completes normally to avoid resource leaks.
How to Trigger Each Error
Trigger exit(3) (无法创建输出文件)
creat() fails when it can't write to the target location. Try these easy scenarios:
- Permission Denied: Run the program as a regular user and try to write to a restricted directory like
/root:./your_program input.txt /root/output.txt - Filename is an Existing Directory: Try to create a file with the same name as an existing folder:
mkdir existing_dir ./your_program input.txt existing_dir
Trigger exit(4) (读取输入文件时出错)
Since read() only returns -1 on actual errors (not empty files), use one of these controlled methods:
Method 1: Use a Debugger (GDB)
- Compile your code with debug symbols:
g++ -g your_code.cpp -o your_program - Start GDB:
gdb ./your_program - Set a breakpoint at the
read()line (replace25with the actual line number in your code):break main:25 - Run the program with valid input/output files:
run input.txt output.txt - When the breakpoint hits, force
read()to return -1:set var read_size = -1 - Continue execution:
The program will print the error message and exit with code 4.c
Method 2: Revoke Permissions Mid-Run
- Add a 10-second delay after opening the input file to give yourself time to change permissions:
in_file = open(argv[1], O_RDONLY); if (in_file < 0) { ... } sleep(10); // Add this line - Compile and run the program:
./your_program input.txt output.txt - While the program sleeps, open another terminal and remove read permissions from the input file:
chmod 000 input.txt - After the sleep ends,
read()will fail due to missing permissions, triggering exit(4).
内容的提问来源于stack exchange,提问作者Lexi Carnell
相关产品推荐
相关产品推荐

