samedi 27 février 2021

What should the error statements be for exit 3 and 4 in my c++ simple buffer code?

I need to input error statements above exits three and four and then actually force the code to go to these error statements so I can have screenshot proof that they are working. However, I can't quite work out what should be in each. My initial thoughts are 'the output file can't be created for 3 and 'The file you want to read from is empty' for 4, but I can't seem to trigger these errors so I feel like that's not correct.

#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<<"The command-line input does not contain 3 arguments"<<endl;
        exit(1);
        }
     in_file= open(argv[1], O_RDONLY);
     if (in_file  < 0) {
        cout<<"The file you are trying to copy from doesnt exist"<<endl;
        exit(2);
        }
    out_file = creat(argv[2], OUTPUT_MODE);
    if (out_file < 0) {
        cout<<"Error statement 3"<<endl;
            exit(3);
        }
    while (read_size > 0) {
            read_size = read(in_file, buf, BUF_SIZE);
            if (read_size <0){
            cout<<"Error statement 4"<<endl;
                exit(4);
            }
            write_size = write(out_file, buf, read_size);
            if (write_size<=0){
                close(in_file);
                close(out_file);
                cout<<"Reading and writing from and to files is complete"<<endl;
                exit(5);
               }
             }
}

Aucun commentaire:

Enregistrer un commentaire