Find the answer to your Linux question:
Results 1 to 8 of 8
Hi, (i'm using c on linux) i'm tryint to print to a file by using : double time1 = difftime(end,start); char* cmd; sprintf(cmd, "echo \"time is ${time1} \" > /tmp/try"); ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2012
    Posts
    4

    system()


    Hi, (i'm using c on linux)

    i'm tryint to print to a file by using :

    double time1 = difftime(end,start);
    char* cmd;
    sprintf(cmd, "echo \"time is ${time1} \" > /tmp/try");
    system(cmd);

    but it prints to /tmp/try only :
    time is

    without the value of the time1 variable..
    what can be the problem ?
    thanks

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,846
    The problem is you're embedding your C variable into a string of text that is processed by bash (or whatever shell you're running). It cannot see the variable, you're outside of its scope when the variable is read, hence you're seeing nothing printed.

    Try the C version of the parameter output...

    Code:
    sprintf(cmd, "echo \"time is %Lf \" > /tmp/try", time1);
    The parameter formats are all listed here.
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Just Joined!
    Join Date
    Jan 2012
    Posts
    4

    d

    Hi,
    i tried that, but it doesn't creat a file /tmp/try ..

  4. #4
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,846
    It doesn't create a file now? Even though it did when you did it above? Does it give you any error message when you run it?
    Linux user #126863 - see http://linuxcounter.net/

  5. #5
    Just Joined!
    Join Date
    Jan 2012
    Posts
    4
    it seems that it creates a file when i write:
    char cmd[256]

    instead of

    char* cmd

    .. weird.. ?

  6. #6
    Just Joined! pieman's Avatar
    Join Date
    Jan 2012
    Location
    Sunny Yorkshire
    Posts
    11
    If you just declare char* cmd, you have not allocated any memory to write your string into.

    Declaring char cmd[256] declared 256 chars that you can write into.

    You need to declare memory before writing into it, so in this instance, char cmd[256] is the correct way to go.

  7. #7
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,846
    No, not wierd. I thought your code sample above was just that - a sample, excepts from your code to show what was happening. I didn't realise you were actually running it as-is.

    If you declare a C pointer as:

    Code:
    char * variable;
    The compiler will allocate the pointer in the heap (for globals) or on the stack (for locals) from the next available bit of memory. As a pointer you get enough bytes allocated to hold an address. But unless you tell it you want your pointer to point at some specific memory address, it'll just hold whatever those bytes were already in that memory. That could be anywhere.

    This, as you can probably appreciate, is quite dangerous - you should only be using the pointer after you've initialised it. You need to point it at some memory somewhere - if you don't, then when you write numbers to the memory it points at, you could be dumping over anything.

    When you create a pointer variable, always point it at the NULL address:
    Code:
    char * variable = 0;
    That way, if you accidentally write to the pointer before you point it at something, your program will blow up with a nice, safe crash rather than dump randomly over memory doing harm and causing odd effects - symptoms that would have no logic or follow any pattern.
    Linux user #126863 - see http://linuxcounter.net/

  8. #8
    Just Joined!
    Join Date
    Jan 2012
    Posts
    4
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •