Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-11-2004, 10:04 PM   #1
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?



Old 04-11-2004, 10:15 PM   #2
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
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)

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


Old 04-12-2004, 08:28 AM   #4
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.


// 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;
Contents of blah.txt:
$./test <blah.txt >results.txt
$ cat result.txt
Enter some text:
You entered: blah


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 > Forums > Non-*NIX Forums > Programming

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

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