Copying the contents of two files to a third file

I wasn't sure whether to put this here or in beginners, bascially I am studying some c++ as part of one of my university modules. Unfortunately the lecturer doesn't seem to be very good at explaining things very well. I have had a look at this myself and I really don't know where to begin, its mostly because I don't understand what the parts of the current program do because the lecturer hasn't really explained it. If someone could help me by pointing out what each bit of the code does I would be very grateful.

The task is to take the following program and edit it so that it takes 3 arguments, two naming existing files and the third the name of a new file to be created. The program should copy the entire contents of the first named file to the new file followed by copying the entire contents of the second file to the end of the new output file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>

using namespace std;
const int bufsiz = 4096;

int main(int argc, char *argv[]){
  int fin, fout, rc;
  char buffer[bufsiz];

  if(argc != 3) {
    cerr << "need 2 filenames\n"; exit(1);
  }
  fin=open(argv[1],O_RDONLY);
  if(fin < 0) {
    cerr << "cannot open " <<  argv[1] << "\n"; exit(1);
  }
  fout=open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0644);
  if(fout < 0) {
    cerr << "cannot open " <<  argv[2] << "\n"; exit(1);
  }
  rc = read(fin, buffer, bufsiz);
  while(rc > 0) {
    write(fout, buffer, rc);
    rc = read(fin, buffer, bufsiz);
  }
  close(fout);
}


I am aware there are probably a number of different ways of doing this but if we could stick to the same sort of convention used here that would be helpful.

Thanks in advance,
Phil
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>

using namespace std; //namespace
const int bufsiz = 4096; //blah blah...

int main(int argc, char *argv[]){
  int fin, fout, rc; //create the ints
  char buffer[bufsiz]; //create a char* called buffer that is bufsiz in length

  if(argc != 3) { //if there are not 3 arguments (argc means "arg count")
    cerr << "need 2 filenames\n"; exit(1); //display error/exit
  }
  fin=open(argv[1],O_RDONLY); //open the file described in argv[1] (argument 1) with readonly access and store it to fin
  if(fin < 0) { //if fin < 0 (due to an error in open I assume)
    cerr << "cannot open " <<  argv[1] << "\n"; exit(1 //display error, exit
  }
  fout=open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0644); //open the new file for read/write, create it if it doesn't exist, and delete stuff it in if it has stuff in it
  if(fout < 0) { //if it errored
    cerr << "cannot open " <<  argv[2] << "\n"; exit(1);
  }
  rc = read(fin, buffer, bufsiz); //read data into char* (as much as possible)
  while(rc > 0) { //as long as it is getting data
    write(fout, buffer, rc); //write data to the file
    rc = read(fin, buffer, bufsiz); //read more
  }
  close(fout); //close the file
  //implied return 0;
}
Thanks very much, the comments have made it much easier to understand and I think I should be able to do it now.

Phil
Topic archived. No new replies allowed.