-5

Sorry for repost. Have another 2 codes and with 1 problem. Both of these codes will show me up "Error in parameters" when I am going to run them.

First code -

#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);
}

Second one, has the same problem when I gonna run it "Error in parameters"

#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);
}

Sorry again for repost....

user6200539
  • 3
  • 1
  • 2

1 Answers1

3

You must give to your program 3 arguments. This is caused by this line

if(argc < 3) {

which signify "if the number of arguments is below 3".

Edit :

This line is wrong because you need 4 parameters as the program name is the first parameter. Change this line to

if(argc < 4) {

And just call your program this way :

./program file1 file2 file3
Boiethios
  • 38,438
  • 19
  • 134
  • 183