Redirecting stdout

This is a discussion on Redirecting stdout within the C Programming forums, part of the General Programming Boards category; Hello everyone, I'm executing the following command on the terminal : Code: find . | ./so du The output is ...

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Redirecting stdout

    Hello everyone,
    I'm executing the following command on the terminal :

    Code:
    find . | ./so du
    The output is something like this :

    Code:
    32	./GuardaLinhas
    288	.
    16	./.DS_Store
    32	./GuardaLinhas
    16	./GuardaLinhas/.DS_Store
    8	./GuardaLinhas/a
    8	./GuardaLinhas/o
    24	./mapreduce
    8	./mapreduce.c
    8	./processosacorrer.rtf
    24	./so
    8	./so.c
    8	./sotrab.txt
    8	./sotrab2.txt
    24	./testeso
    8	./testeso.c
    120	./tp-lcc.pdf
    I want all of this output to be sent to a textfile.
    Is this possible ?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by anduril462 View Post
    Thanks, will study it and post any doubts I have.

  4. #4
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    These are all commands I can use on the terminal but I need to able to redirect the output of the command I insert in the terminal to a txt.

    I'm trying to do it like this :

    Code:
    fp = freopen ("/Users/machd/Desktop/SistemasOperativos2013/stdout", "a+", stdout);
    And it works but it sends it to that txt and the rest doesn't appear in the terminal.
    Last edited by DeanWinchester; 05-24-2013 at 06:08 PM.

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    If you don't want to code that functionality into your program you can use tee, otherwise you could look at fopen and do an extra write to a file for each one you do to stdout.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why not use

    Code:
    find . | ./so du >mytextFile
    ?

  7. #7
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by Adak View Post
    Why not use

    Code:
    find . | ./so du >mytextFile
    ?
    Because that's not the point.
    I need to code it.

    The output is generated due to this :

    Code:
    tam=strlen(str);
    str[tam-1]='\0';  
    map(argv[1],str);
    The function map runs an execl

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,230
    O_o

    Well, if I understand correctly, I vote for "tee" like Barney McGrew suggested: find . | ./so du | tee /dev/tty textfile

    If you are saying instead that your application proper needs to write to `stdout' while redirecting the spawned children you have to do that with your code for your executable.

    1): Process a particular CLI argument as the target filename defaulting to `stdout'. (your_exe -o textfile)
    2): Copy the `stdout' descriptor for your own use so that you may continue to write whatever you want to the real `stdout'.
    3): Use `open' with `dup2', or related API, before `execl' so that the child process inherits a different descriptor for `stdio'.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting stdout
    By Soel in forum C Programming
    Replies: 3
    Last Post: 01-30-2011, 02:29 PM
  2. redirecting stdout
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2006, 09:20 PM
  3. Redirecting stdout from XEV
    By 3saul in forum Linux Programming
    Replies: 3
    Last Post: 02-28-2006, 01:22 AM
  4. redirecting stdout
    By gregulator in forum C Programming
    Replies: 2
    Last Post: 04-22-2004, 10:07 AM
  5. redirecting stdout
    By Draco in forum C Programming
    Replies: 13
    Last Post: 10-11-2003, 12:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21