我需要使用Linux的系统调用完成一个练习:
- 打开文件“testinput.txt”(已经存在于工作目录中,不为空)
- 打开文件“testoutput.txt”(在工作目录中不存在)
- 将testinput的内容复制到testoutput中
- 关闭两个文件
我发现可以使用系统调用open
打开输入文件(标志O_RDONLY
)和输出文件(标志O_CREAT | O_WRONLY
)。
输入文件能正常工作。但输出文件不能。
错误代码是2 = “No such file or directory”(显然,我要求创建文件)。我做错了什么?
P.S.:为了强调我正在使用系统调用,我没有调用函数open(...)
,而是syscall(SYS_OPEN, ...)
outFileDesc = syscall(SYS_OPEN, "testoutput.txt", O_WRONLY | O_TRUNC | O_CREAT, 438);
if(outFileDesc == -1) {
int code = errno;
// gdb used here -> print code -> 2
char str[] = "Unable to open \"testoutput.txt\"\n";
syscall(SYS_WRITE, FILE_DESC_STDOUT, str, sizeof(str)/sizeof(char));
syscall(SYS_CLOSE, inFileDesc);
syscall(SYS_EXIT, 1);
return 1;
}
整个代码:
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
/* file flags fcntl.h */
#define O_RDONLY 0x0000 /* open for reading only */
#define O_WRONLY 0x0001 /* open for writing only */
#define O_CREAT 0x0200 /* create if nonexistant */
#define O_TRUNC 0x0400 /* truncate to zero length */
#define FILE_DESC_STDOUT 1
#define SYS_EXIT 1
#define SYS_READ 3
#define SYS_WRITE 4
#define SYS_OPEN 5
#define SYS_CLOSE 6
#define IN_MESSAGE_LEN 9
#define OUT_MESSAGE_LEN 9
char inBuffer[IN_MESSAGE_LEN];
char outBuffer[OUT_MESSAGE_LEN];
int main (int argc, char *argv[])
{
int inFileDesc, outFileDesc;
// Apertura fine di input
inFileDesc = syscall(SYS_OPEN, "testinput.txt", O_RDONLY, 438);
if(inFileDesc == -1) {
char str[] = "Unable to open \"testinput.txt\"\n";
syscall(SYS_WRITE, FILE_DESC_STDOUT, str, sizeof(str)/sizeof(char));
syscall(SYS_EXIT, 1);
return 1;
}
// Apertura fine di output
outFileDesc = syscall(SYS_OPEN, "testoutput.txt", O_WRONLY | O_TRUNC | O_CREAT, 438);
if(outFileDesc == -1) {
int code = errno;
char str[] = "Unable to open \"testoutput.txt\"\n";
syscall(SYS_WRITE, FILE_DESC_STDOUT, str, sizeof(str)/sizeof(char));
syscall(SYS_CLOSE, inFileDesc);
syscall(SYS_EXIT, 1);
return 1;
}
// Travaso contenuto file di input in file di output
int read, i;
while((read = syscall(SYS_READ, inFileDesc, inBuffer, IN_MESSAGE_LEN)) != 0) {
for(i = 0; i < IN_MESSAGE_LEN; i++) {
outBuffer[i] = inBuffer[i];
}
syscall(SYS_WRITE, outFileDesc, outBuffer, OUT_MESSAGE_LEN);
}
syscall(SYS_CLOSE, inFileDesc);
syscall(SYS_CLOSE, outFileDesc);
syscall(SYS_EXIT, 0);
return 0;
}
open()
而不是通过syscall()
来执行,你能够成功地创建(并写入)输出文件吗? - user707650#include <fcntl.h>
? - jxh