Help with writing a tee program in 4 hours

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with writing a tee program in 4 hours
# 1  
Old 11-14-2012
Help with writing a tee program in 4 hours

1. The problem statement, all variables and given/known data:
Basic Assignment
Write a program similar to the Unix "tee" command.
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 | lpr -P csl

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 lpr side of the pipe.
The -a option to the "tee" command changes the functionality slightly so that instead of overwriting the output files, the data stream is appended to the output files. Your program should also implement this functionality.
Getting a line
The read() and write() functions work with file descriptors and have no understanding of lines in the file. You must write a helper function which will read input by lines.
The following function signature is recommended:

int getline(int fd, char * buffer, int max_size)

The file descriptor is the open file to be read from. The buffer is an array of characters allocated by you to hold the line. The buffer should have max_size allowable characters. The function returns the number of characters actually read.
The line returned in the buffer should NOT have the newline character stored in it.

Your program should be called "z123456.tee" where z123456 should be replaced with your z-ID.
Input
A sample input file can be found here (synclog) .
Error Checking
If any output file cannot be opened, then that file should be skipped. Remaining files should be processed.

Output
Code:
% 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
%

2. Relevant commands, code, scripts, algorithms:

3. The attempts at a solution (include all code and scripts):
Code:
(
read(){
int getline(int fd, char * buffer, int max_size)
int fd, count;
char buffer[max_size];
fd = open("inputfile", O_RDONLY);
if (fd == -1) {
cerr << "file could not be opened\n";
exit (fd)
}
count = read(fd, buffer, max_size);
cout << "read" << count <<" bytes from file\n;
close(fd);
return 0;

write(){
int getline(int fd, char * buffer, int max_size)
int fd, count;
char buffer[] = 
fd = open(" ", O_WRONLY);
if (fd == -1) {
cerr << fild could not be opened\n";
exit (fd);
}
count = write(fd, buffer, sizeof(buffer));
cout << "wrote " << count <<" bytes to file\n";
close(fd);
return 0;

)

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course)
niu, dekalb, Il, ege, 330
).

---------- Post updated at 03:17 PM ---------- Previous update was at 01:00 PM ----------

Quote:
Originally Posted by izzy077
1. The problem statement, all variables and given/known data:
Basic Assignment
Write a program similar to the Unix "tee" command.
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 | lpr -P csl

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 lpr side of the pipe.
The -a option to the "tee" command changes the functionality slightly so that instead of overwriting the output files, the data stream is appended to the output files. Your program should also implement this functionality.
Getting a line
The read() and write() functions work with file descriptors and have no understanding of lines in the file. You must write a helper function which will read input by lines.
The following function signature is recommended:

int getline(int fd, char * buffer, int max_size)

The file descriptor is the open file to be read from. The buffer is an array of characters allocated by you to hold the line. The buffer should have max_size allowable characters. The function returns the number of characters actually read.
The line returned in the buffer should NOT have the newline character stored in it.

Your program should be called "z123456.tee" where z123456 should be replaced with your z-ID.
Input
A sample input file can be found here (synclog) .
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
%
2. Relevant commands, code, scripts, algorithms:

3. The attempts at a solution (include all code and scripts):
(
read(){
int getline(int fd, char * buffer, int max_size)
int fd, count;
char buffer[max_size];
fd = open("inputfile", O_RDONLY);
if (fd == -1) {
cerr << "file could not be opened\n";
exit (fd)
}
count = read(fd, buffer, max_size);
cout << "read" << count <<" bytes from file\n;
close(fd);
return 0;

write()
int getline(int fd, char * buffer, int max_size)
int fd, count;
char buffer[] =
fd = open(" ", O_WRONLY);
if (fd == -1) {
cerr << fild could not be opened\n";
exit (fd);
}
count = write(fd, buffer, sizeof(buffer));
cout << "wrote " << count <<" bytes to file\n";
close(fd);
return 0;

)

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course)
niu, dekalb, Il, ege, 330
).

Last edited by vbe; 11-15-2012 at 05:39 AM.. Reason: code tags + missing {
# 2  
Old 11-15-2012
What is the issue? (The missing { in red? )
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

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... (0 Replies)
Discussion started by: assignmentoper
0 Replies

2. Homework & Coursework Questions

Need help writing a shell script program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write the a shell script program to remove all space characters stored in the shell variable TEXT.... (7 Replies)
Discussion started by: kofine05
7 Replies

3. Programming

Writing C++ program Arguments and Classes

I want to write a C++ program that uses a class to do some calculations. I pass arguments to the program, some of which are used to set up class members. A class function will then perform the necessary calculations. I am wondering how I should pass the arguments from the program to set the... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Programming

writing a shell program in ubuntu

Hi all, I have an assignment from school to write a shell program in linux. the idea is to exercise fork() and execv() functions.. the shell program is supposed to be the master and every command that the user prints will run in a new process. we also need to try running the command in every... (1 Reply)
Discussion started by: r3vive
1 Replies

5. UNIX Desktop Questions & Answers

need help writing a program to look for doubles

to determine if two two doubles are equal, we check to see if their absolute difference is very close to zero. . .if two numbers are less than .00001 apart, theyre equal. keep a count field in each record (as you did in p5). once the list is complete, ask the user to see if an element is on... (2 Replies)
Discussion started by: rickym2626
2 Replies

6. UNIX for Dummies Questions & Answers

Problem with writing a program

Hi guys I'm having trouble with trying to create a script which calculates the grade of a student and the marks out of 300. The grades are: 0-49% fail 50-59% pass 60-69% credit pass 70-79% distinction 80-100% high distinction less than 0 or greater than 100 displays error message. My... (1 Reply)
Discussion started by: CompNoob
1 Replies

7. Shell Programming and Scripting

writing math testing program

Can anyone create a program that would test the math skills of the user. Assume that it would test integer addition, The program should ask the question,collect the integer response, evaluate and notify the user if their answer was correct or incorrect. I would assume integers in the range... (5 Replies)
Discussion started by: ccp
5 Replies

8. Shell Programming and Scripting

Need help on a program I'm writing

Hey guys.. I'm trying to learn how to script in bash... THIS IS NOT AN ASSIGNMENT but my instructor says to learn you must practice I'm trying to add to a program I'm writing that will print and save raw data... What syntax commands would I use to write them? And Thank you... (2 Replies)
Discussion started by: dmosheye
2 Replies

9. UNIX for Dummies Questions & Answers

writing a program

i have written a very simple program in the vi editor, how do i now make it an executable file? (3 Replies)
Discussion started by: carlvernon
3 Replies

10. What is on Your Mind?

I'm writing a new Linux program!

Yep, that's right. I'm writing a Linux binary that requires an X11 Server. It will also be released in a Shell, Win32, and Cocoa (Mac OS X). It's a program that's a text editor and more. It not just creates TXT and RTF files, it also can save in XML, RSS, and a whole lot of other formats. ... (11 Replies)
Discussion started by: Danny_10
11 Replies
Login or Register to Ask a Question