Thread: sending output from a command to file

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    sending output from a command to file

    Hi,

    I require to send an output from a unix command such as date, and store it into a file. The problem i have is that i cannot get the output from date into a char array.

    Can anyone help me on this ..

    Thankyou

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    FILE *fp = popen( "date", "r" );
    Now read the output of date as if it were a file
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Not sure if this is the best method but it works for me :

    Code:
    #include <stdio.h>
    
    int main(void)
    {
     FILE *filepointer = popen("date", "r");
     char output[30];
    
     while(fgets(output,sizeof(output),filepointer));
    
     printf("Today is - %s", output);
    
     pclose(filepointer);
    
     return 0;
    }
    Hope this helps.

    Stan

    *edit sorry didnt see salem had already replied to this, I seem to experience quite a bit of lag on this board as it took me ages to get my message posted.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    If you don't need a program for it
    Code:
    %ls -a > file.txt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  2. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  3. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM