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");
...
- 01-30-2012 #1Just 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
- 01-30-2012 #2
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...
The parameter formats are all listed here.Code:sprintf(cmd, "echo \"time is %Lf \" > /tmp/try", time1);
Linux user #126863 - see http://linuxcounter.net/
- 01-30-2012 #3Just Joined!
- Join Date
- Jan 2012
- Posts
- 4
d
Hi,
i tried that, but it doesn't creat a file /tmp/try ..
- 01-30-2012 #4
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/
- 01-30-2012 #5Just 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.. ?
- 01-30-2012 #6
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.
- 01-30-2012 #7
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:
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.Code:char * variable;
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:
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.Code:char * variable = 0;
Linux user #126863 - see http://linuxcounter.net/
- 01-31-2012 #8Just Joined!
- Join Date
- Jan 2012
- Posts
- 4
thanks


Reply With Quote