LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-30-2012, 07:06 AM   #1
sowpra
LQ Newbie
 
Registered: Feb 2012
Posts: 23
Blog Entries: 1

Rep: Reputation: Disabled
Question How to take a system() value into a local variable??


hi..!

Is there any way to assign a system value into a local variable in 'C' ???

for eg : system("date +%T") will give us the current time...but as i wanted only the minute value...we used system("date +%M")..this prints the minute value...but i want to assign this value to a local variable...like....

int i;
i = system("date +%M");
printf("%d",i);

but this is not happening...system() value are not getting assigned..!
Can anybody help us with this??

thanks
 
Old 04-30-2012, 07:56 AM   #2
wildwizard
Member
 
Registered: Apr 2009
Location: Oz
Distribution: slackware64-14.0
Posts: 875

Rep: Reputation: 282Reputation: 282Reputation: 282
Not sure why you think this is the way to go about it.

system() executes another program as though you typed it into the command prompt the return value is the error code returned nothing else.

In C you use gettimeofday() which gives you the time in seconds, you then use ctime() to convert it into seconds/minutes/hours etc

see the manual page for gettimeofday and ctime
 
Old 04-30-2012, 10:08 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by sowpra View Post
...
but this is not happening...system() value are not getting assigned..!
...
Why do you think so ? Can you present any proof ?
 
Old 05-02-2012, 12:29 AM   #4
sowpra
LQ Newbie
 
Registered: Feb 2012
Posts: 23

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
Why do you think so ? Can you present any proof ?
Hi!!
Its because..after assigning the value to a local variable..i tried printing it..but its getting printed as 0..!
 
Old 05-02-2012, 02:02 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Like Wildwizard and Sergei Steshenko clearly told you, sowpra, the system() function call works. When the command specified executes successfully, system() will return zero.

The problem is that you somehow expect system() to capture the output, and give it to you as an integer. It does not do that.

To capture the output from a command, use popen(). It starts the command, and captures its output as a file-like stream (if given "r" in the second parameter). popen() returns the handle for that stream. You need to keep reading from the stream, otherwise the kernel will "pause" (proper term is "block") the command until your process consumes the stream.

To capture the output of a command as an integer, you could use for example
Code:
#define  _POSIX_C_SOURCE  200809L
#include <stdio.h>
#include <errno.h>

/* Execute the specified command, and parse the output as an integer.
 * result can be NULL, if you don't need the integer for anything.
 * Function returns 0 if successful, error code (errno) otherwise.
*/
int run_and_parse_int(const char *const command, int *result)
{
    FILE   *cmd;
    int     c, value, saved_errno;

    /* Save errno, so we can keep it unchanged if successful. */
    saved_errno = errno;

    /* Invalid command? */
    if (!command | !*command)
        return errno = EINVAL;

    /* Start command. */
    errno = ENOMEM;
    cmd = popen(command, "r");
    if (!cmd)
        return errno;

    /* Scan command output for a decimal number. */
    if (fscanf(cmd, "%d", &value) != 1) {
        /* Failed. Kill the command. */
        pclose(cmd);
        return errno = EIO;
    }

    /* Save the result. */
    if (result)
        *result = value;

    /* Ignore the rest of the command output. */
    do {
        c = getc(cmd);
    } while (c != EOF);

    /* Check if the command executed successfully. */
    errno = EIO;
    if (pclose(cmd))
        return errno;

    /* Success. Retain original errno. */
    errno = saved_errno;
    return 0;
}
 
1 members found this post helpful.
Old 05-02-2012, 02:18 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by sowpra View Post
Hi!!
Its because..after assigning the value to a local variable..i tried printing it..but its getting printed as 0..!
And why do you think it shouldn't be 0 ?

Here is 'system' man page: http://linux.die.net/man/3/system . Read it and justify your expectation that the return value shouldn't 0. At all, RTFM.
 
  


Reply



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
need to change variable in func, but var local... raskol Programming 1 04-08-2008 04:23 AM
How can I initialize local/auto variable to zero rahul_kulkarni Linux - General 2 09-07-2007 02:14 PM
Memory for local variable in C on stack shivaligupta Programming 10 01-20-2007 01:22 AM
Bash Local Variable Recursion With Array jshivers Programming 0 06-16-2006 04:31 PM
can thread return a local variable? iclinux Programming 1 01-13-2005 11:37 PM

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

All times are GMT -5. The time now is 03:59 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