shell() and variable

How to use variable in shell()???
I'm not trying to save value in file, this is just example...

I been googling with no luck. Trying here, please help....

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <stdlib.h>

main() {
    char *filetosave = "cron";

    printf("Save in: %s", filetosave);
    system("echo > $filetosave");
}
Use getenv() to read an environment variable:

1
2
3
4
5
6
7
#include <iostream>
#include <cstdlib>

main() {
    const char* user = getenv( "USER" );
    std::cout << "The current user is " << ( user ? user : "Unknown" ) << std::endl;
}


(Note that getenv() returns a null pointer if the specified environment variable is not set.)
I think what you mean is how to use the C variable filetosave in the system() call. My C is very bad, as I've been spoilt by iostreams and std::string, but the function you want is snprintf, and it is used something like this:

1
2
3
char buffer[1000];
snprintf(buffer, sizeof(buffer), "echo > %s", filetosave);
system(buffer);

Yup, thanks kev82!
Solved my prob so far.... =)
Topic archived. No new replies allowed.