Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Here's my problem, when I try to do

long int second;
second=system("date +%s);

and then export the "second" to a text file i get 0 instead of 1403109...

How can I export the output of this command to a text whenever the second changes?

share|improve this question

closed as off-topic by ændrük, Sneetsher, bain, Eric Carvalho, bodhi.zazen Jun 19 '14 at 17:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – ændrük, Sneetsher, bain, Eric Carvalho, bodhi.zazen
If this question can be reworded to fit the rules in the help center, please edit the question.

Please see the documentation of system() function. It returns the exit code of the command (here date) and not the output of it. Also, it would be a bad practice to use a system call in order to take the current second since 1970, but you could use something from time.h stackoverflow.com/questions/2242963/… –  hakermania Jun 18 '14 at 16:42

3 Answers 3

up vote 0 down vote accepted

The output of system("date +%s") can not be stored in a variable as you tried.

From man system

int system(const char *command);

system executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.

system does not return the output of the command executed. It returns 0 if command is successfully executed and shell is not available. Otherwise returns some integer exit status.

How can I export the output of this command to a text file

Method-1: Redirection (already mentioned by @Jobin): call system as,

system("date +\%s >> text.txt");

Method-2: Use popen, It will return a FILE* which you can use to read the output of the command.

#include <stdlib.h>
#include <stdio.h>

int main()
  FILE *fp,*outputfile;
  char var[40];

  fp = popen("date +%s", "r");
  while (fgets(var, sizeof(var), fp) != NULL) 
      printf("%s", var);

  outputfile = fopen("text.txt", "a");

  return 0;
share|improve this answer

To get time you could use functions in time.h System function returns only return code, instead of output.

#include <iostream>
#include <stdio.h>

int main()
    FILE* outputfile = popen(REPLACE_WITH_COMMAND, "r");
    // Now, in the variable outputfile, is output as pointer
    //   to FILE object. Operate with this as file
    return 0;
share|improve this answer
how can i export this to cout or text.txt? Iam interested in finding when the output changes value and save the new value in a text..thx –  mrapsogos Jun 18 '14 at 17:50

You could directly redirect the output to a file using the redirection operator > as follows:

system("date +\%s > filename");

Remember you need to escape the % and replace filename by the file you want the output to redirect to.

The > operator redirects the output of the command to the left hand side to the file on the right hand side by overwriting the contents of filename (if any) with the output, instead of printing it to standard output. If you want to append the output instead of overwriting the previous contents, you could use >> instead of >.

share|improve this answer
THANK YOU MY FRIEND:) now can you lead me how to get the content of the file and make condition like "if(filename1!=filename2){filename1=filename2)} –  mrapsogos Jun 18 '14 at 18:10
@user1: That would form a new, separate question. –  i08in Jun 18 '14 at 18:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.