【发布时间】:2010-10-05 18:14:16
【问题描述】:
对于我的操作系统课程,我的任务是使用系统调用(没有 scanf 或 printf)实现 Unix 的 cat 命令。这是我到目前为止得到的:
(感谢回复编辑)
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
main(void)
{
int fd1;
int fd2;
char *buffer1;
buffer1 = (char *) calloc(100, sizeof(char));
char *buffer2;
buffer2 = (char *)calloc(100, sizeof(char));
fd1 = open("input.in", O_RDONLY);
fd2 = open("input2.in", O_RDONLY);
while(eof1){ //<-lseek condition to add here
read (fd1, buffer1, /*how much to read here?*/ );
write(1, buffer1, sizeof(buffer1)-1);
}
while (eof2){
read (fd2,buffer2, /*how much to read here?*/);
write(1, buffer2, sizeof(buffer2)-1);
}
}
我看到的例子只显示了已知字节数的读取。我不知道每个读取文件会有多少字节,那么如何指定读取的最后一个参数呢?
【问题讨论】:
-
我的answer 到您的previous question 可能与您仅使用系统调用实现cat 相关。
标签: c system-calls cat