很抱歉重新发布。还有另外2个代码,但有1个问题。当我要运行它们时,这两个代码都将向我显示“参数错误”。
第一个代码-
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/file.h>
main (argc, argv)
int argc;
char *argv[];
{
int fd1, fd2, fd3;
int nbytes, mode, nbytes1;
char buf[BUFSIZ];
if(argc < 3) {
fprintf(stderr, "%s: Error in parametrs\n", argv[0]);
exit(1);
}
if ((fd1 = open(argv[1], O_RDONLY)) < 0 ) {
fprintf(stderr, "Can`t open file %s\n",
argv[1]);
exit(1);
}
if ((fd2 = open(argv[2],O_WRONLY | O_CREAT, mode)) < 0) {
fprintf(stderr, "Can`t create new file %s\n", argv[2]);
exit(1);
}
if ((fd3 = open(argv[3], O_WRONLY | O_CREAT, mode)) < 0) {
fprintf(stderr, "Can`t create new file %s\n", argv[3]);
exit(1);
}
while((nbytes = read(fd1, buf, BUFSIZ))> 0) {
if(write(fd2, buf, nbytes) < 0) {
fprintf(stderr, "Write error\n"); break;
}
if(write(fd3, buf, nbytes) < 0) {
fprintf(stderr, "Write error\n"); break;
}
}
if(nbytes < 0) fprintf(stderr, "Reading error\n");
close(fd1);
close(fd2);
close(fd3);
exit(0);
}
第二个,当我运行它时出现同样的问题“参数错误”
#include <unistd.h>
#include <sys/file.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
main (argc, argv)
int argc;
char *argv[];
{
int fd1, fd2, fd3;
int nbytes, mode, nbytes1;
char buf[BUFSIZ];
if(argc < 3) {
fprintf(stderr, "%s: Error in parametrs\n", argv[0]);
exit(1);
}
if ((fd1 = open(argv[1], O_RDONLY)) < 0 ) {
fprintf(stderr, "Can`t open file %s\n",
argv[1]);
exit(2);
}
if ((fd2 = open(argv[2],O_WRONLY | O_CREAT, mode)) < 0) {
fprintf(stderr, "Can`t create new file %s\n", argv[2]);
exit(3);
}
if ((fd3 = open(argv[3], O_WRONLY | O_CREAT, mode)) < 0) {
fprintf(stderr, "Can`t create new file %s\n", argv[3]);
exit(4);
}
while((nbytes = read(fd1, buf, BUFSIZ))> 0) {
if(write(fd2, buf, nbytes) < 0) {
fprintf(stderr, "Write error\n");
break;
}
if(write(fd3, buf, nbytes) < 0) {
fprintf(stderr, "Write error\n");
break;
}
}
if(nbytes < 0) fprintf(stderr, "Reading error\n");
close(fd1);
close(fd2);
close(fd3);
exit(0);
}
再次抱歉,要重新发布...。
您必须给您的程序3个参数。这是由这条线引起的
if(argc < 3) {
表示“如果自变量数量小于3”。
编辑 :
这行是错误的,因为您需要4个参数,因为程序名称是第一个参数。将此行更改为
if(argc < 4) {
并以这种方式调用您的程序:
./program file1 file2 file3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句