C++ with Linux - writing a "tee"-like function

Greetings, everyone.

  1. The problem statement, all variables and given/known data:

I'm running into a problem with my program concerning the actual output it does. When I open the file that gets the output, it contains
a large number of hex(?) variables and not what the user wants. The problem should be with the read statement, as it seems the write statement works fine.

The user is supposed to enter something like:

 cat date | myProgram.tee file1 file2 file3 > file4

via the command line

I don't understand how exactly I would get the input from the file being piped (in this example: cat date). I assumed just having the
read statement would automatically take in whatever is passed to it, but I guess that's not correct.

I have contacted the professor but his answers did not help at all.

P.S., we HAVE to use read and write for this particular assignment, so any alternative functions would not be acceptable on submission.

  1. Relevant commands, code, scripts, algorithms:

Write a program similar to the Unix "tee" command.
Due Wednesday April 10 at class time.
The assignment is worth 100 points.

Program

The Unix "tee" command is used to pull out copies of a data stream. It is typically used in conjunction with pipes
(analogous to a T-joint in plumbing, hence the name). It takes all data from the standard input and copies it to the
standard output, but also copies it into files named as its arguments. For example, the command

cat myfile | tee a b c | wc -l  

would take the data coming from the cat side of the pipe and store it in the files a, b, and c, in addition to sending it on to the wc side of the pipe.
The -a option to the "tee" command changes the functionality: instead of overwriting the output files, the data stream is appended to the output files. Your program should also implement this functionality.
Your program should be called "z123456.tee" where z123456 should be replaced with your z-ID.
Input

A sample input file can be found [can't link]
Error Checking

If any output file cannot be opened, then that file should be skipped. Remaining files should be processed.
Output

% z123456.tee Usage: z123456.tee [-a] out_file1 [ out_file2 ...] Sends lines of the standard input to all of the output files
and to the standard output. The -a option will append the output to all files instead of overwriting them.

% cat synclog | z123456.tee a b c > d 
% ls -l 
total 268 
-rw-r--r-- 1 user user 37432 Nov  5 16:31 a 
-rw-r--r-- 1 user user 37432 Nov  5 16:31 b 
-rw-r--r-- 1 user user 37432 Nov  5 16:31 c 
-rw-r--r-- 1 user user 37432 Nov  5 16:31 d 
-rw-r--r-- 1 user user 37432 Nov  5 16:20 synclog
% diff a synclog % cat synclog | z123456.tee -a a b c > d  
% ls -l 
total 388 
-rw-r--r-- 1 user user 74864 Nov  5 16:34 a 
-rw-r--r-- 1 user user 74864 Nov  5 16:34 b 
-rw-r--r-- 1 user user 74864 Nov  5 16:34 c 
-rw-r--r-- 1 user user 37432 Nov  5 16:34 d 
-rw-r--r-- 1 user user 37432 Nov  5 16:20 synclog

Other Points

  • make sure that your assignment is contained in a single file called "z123456.cxx" based on your Z-id
  • make sure that your program compiles, links and runs fine on your Linux system, turing or hopper.

Submission

Submit your C++ source code file via Blackboard below.

  1. The attempts at a solution (include all code and scripts):

Here's what I have so far:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    int fd;
    char buffer[1024];
    // open existing file, use extra O_CREAT for new file
    if(strcmp("-a",argv[1]) == 0) //if the user enters -a
    {
        for(int i = 2; i < argc; i++)    
        {
            fd = open(argv, O_RDWR | O_APPEND);
            if (fd == -1) {
                cerr << "file could not be opened\n";
                exit(fd);
            }
            //get the input
            read(fd, buffer, 1024);    
            // write to file
            write(fd, buffer, sizeof(buffer));    
            // close file
            close(fd);
        }
    }
    else
    {
        for(int i = 1; i < argc; i++)    
        {
            fd = open(argv, O_RDWR);
            if (fd == -1) {
                cerr << "file could not be opened\n";
                exit(fd);
            }
            //get the input
            read(fd, buffer, 1024);    
            // write to file
            write(fd, buffer, sizeof(buffer));    
            // close file
            close(fd);
        }
    }
    return 0;
}
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Northern Illinois University, Dekalb (IL), United States, Ege, UNIX 330

Thanks, guys!