sending output from a command to file

This is a discussion on sending output from a command to file within the C Programming forums, part of the General Programming Boards category; Hi, I require to send an output from a unix command such as date, and store it into a 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
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 02:11 AM
  2. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05: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, 04:28 PM

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