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


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions C++ with Linux - writing a "tee"-like function
# 1  
Old 04-10-2013
Question 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:
Code:
 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.

2. 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
Code:
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.
Code:
% 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.


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

Here's what I have so far:
Code:
#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[i], 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[i], 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;
}

4. 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!

Last edited by vbe; 04-11-2013 at 06:31 AM.. Reason: more formatting + typo
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mindboggling difference between using "tee" and "/usr/bin/tee" in bash

I'm on Ubuntu 14.04 and I manually updated my coreutils so that "tee" is now on version 8.27 I was running a script using bash where there is some write to pipe error at some point causing the tee command to exit abruptly while the script continues to run. The newer version of tee seems to prevent... (2 Replies)
Discussion started by: stompadon
2 Replies

2. UNIX for Beginners Questions & Answers

Append content using "tee" command

Hi, How to append content into a file using tee command echo " file1 is archived"| tee -a archive.txt echo " file2 is archived"| tee -a archive.txt echo " file3 is archived"| tee -a archive.txt how to append content as new rows in the archive.txt Thanks, Srinadh. (4 Replies)
Discussion started by: srinadhreddy27
4 Replies

3. Shell Programming and Scripting

The pipe not use "tee" to print on the screen for specific function

I have code fragment like { aa bb cc } > $LOG aa bb cc, all call function "ff", I want "ff" to print on the screen,but others do not print on the scree, is there a method? I can't use "tee", becasue tee I meet the write "error" ff() { echo "hello" } (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. Shell Programming and Scripting

Screen output is blocked by "| tee" command

BACK STORY: I have a script build.py . (It's for creating the ISO file for a special edition of Swift Linux.) This build.py script executes the mintConstructor.py script that I use to modify the Regular Swift Linux ISO to get the special edition Swift Linux ISO. The lines of the script that... (2 Replies)
Discussion started by: swiftlinux
2 Replies

5. UNIX for Dummies Questions & Answers

Confused over results using "tee" in piped command

First post here, so hopefully all guidelines are followed, and thanks in advance for any replies. I'm working on a shell script(BASH) that processes a csv file and performs various tasks with the data. All is well, except I want to use 'tee' to send output from 'wc' to a file as well as pipe it... (4 Replies)
Discussion started by: jazzmusic
4 Replies

6. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

7. Programming

How to convert the "select" function into a "poll" function

i have a program using the select function but i want to convert it to poll... how can i do this? thanks in advance... :) (1 Reply)
Discussion started by: rbolante
1 Replies
Login or Register to Ask a Question