LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2004, 10:04 PM   #1
serotonincy
LQ Newbie
 
Registered: Jan 2004
Location: Michigan
Posts: 4

Rep: Reputation: 0
Talking Output & input redirection


I have to write a C code for redirection. How do I redirect input and output together like:

cat < read.c > write.c

Also, I'm supposed to write something like:

X a > b < c

I don't even know how to use it. Any ideas?

Thanks.

Serotonincy

 
Old 04-11-2004, 10:15 PM   #2
wolfe2554
Member
 
Registered: Apr 2003
Location: denver co
Distribution: redhat9
Posts: 156

Rep: Reputation: 30
now lets take your first example. the program running is cat. it is getting it's stdinput from read.c then it generates two different out puts. standard out and error out. standard out is being redirected to the write.c if you replaced > with 2> then standard error would get put into write.c and standard out would go to the screen. now < means that it just takes the what ever to the right and puts into whatever is to the left as though you typed it. then > takes whatever comes out of the left and puts it into the right without regards to if anything is there. where as >> appends the out put to the end. another thing is pipes. they look like | , if you were to type cat < read.c | grep to it would run cat take the input from read.c and send the output to grep's input then grep would out put anyline with to in it. try man bash to locate all built in commands and processors.
 
Old 04-12-2004, 01:03 AM   #3
serotonincy
LQ Newbie
 
Registered: Jan 2004
Location: Michigan
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks wolfe.. it makes sense. However, how do I write code for it? If it were just like:
cat < test.c
my code is like this:

- fd = open file test.c for write
- fork it
- parent--> wait for the child to finish
- child --> dup2( fd, STDOUT_FILENO )
close (fd)
execvp

It works fine....But had no clue to redirect the output of cat < test.c to the file t2.c in this argument:

cat < test.c > t2.c

I'm really clueless.........


Thanks,

Sero
 
Old 04-12-2004, 08:28 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Your program doesn't have to handle the file input/output itself. Just make your program read from the standard input, and write to standard output, and it will work fine with redirection. The bash shell takes care of actually redirecting those to a file.

Example:

Code:
// test.c
#include <stdio.h>

int main()
{
    char buffer[256];
    printf("Enter some text:\n");

    scanf("%s", buffer);

    printf("You entered: %s\n", buffer);

    return 0;
}
Code:
Contents of blah.txt:
blah
Code:
$./test <blah.txt >results.txt
$ cat result.txt
Enter some text:
You entered: blah
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing Input Redirection amitbern Programming 3 11-06-2006 12:04 PM
what is output redirection? LinuxPadawan General 5 05-18-2005 12:53 PM
Output redirection rmartine Linux - Newbie 2 01-25-2005 06:31 PM
output redirection to another directory spyghost Linux - Software 3 09-21-2003 09:28 AM
Output redirection legtester Linux - General 4 07-07-2003 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration