Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

basic c++ question

brtkltbrtklt Member Posts: 237
I'm having difficulty with something simple. How would I go about passing a string to a system() function?

char* ch="somefile.txt";
system(?cat ch?);

Thanks for any help.

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    : I'm having difficulty with something simple. How would I go about passing a string to a system() function?
    :
    : char* ch="somefile.txt";
    : system(?cat ch?);
    :
    : Thanks for any help.
    :

    [code]
    char* ch="somefile.txt";
    system(ch);

    OR

    system("somefile.txt");

    OR
    #include
    std::string s = "somefile.txt"
    system(s.c_str());

    [/code]
  • brtkltbrtklt Member Posts: 237
    : : I'm having difficulty with something simple. How would I go about passing a string to a system() function?
    : :
    : : char* ch="somefile.txt";
    : : system(?cat ch?);
    : :
    : : Thanks for any help.
    : :
    :
    : [code]
    : char* ch="somefile.txt";
    : system(ch);
    :
    : OR
    :
    : system("somefile.txt");
    :
    : OR
    : #include
    : std::string s = "somefile.txt"
    : system(s.c_str());
    :
    : [/code]
    :
    I wansn't very descriptive about the problem I'm having, my bad. The problem is I'm trying to cat a file (unix's cat command). For instance to cat the file "file.txt" stored in variable char ch; It's the quotes that are confusing me because you can't quote a variable but quotes are needed for the system file, or am I wrong about that?
  • stoberstober Member Posts: 9,765 ✭✭✭

    Very simple command:
    system("cat filename.txt | more");

    If you want something embedded in quotes:
    system("cat "filename.txt" | more");



  • brtkltbrtklt Member Posts: 237
    :
    : Very simple command:
    : system("cat filename.txt | more");
    :
    : If you want something embedded in quotes:
    : system("cat "filename.txt" | more");
    :
    :
    :
    :
    is there a way to do this with a variable?
  • stoberstober Member Posts: 9,765 ✭✭✭
    : :
    : : Very simple command:
    : : system("cat filename.txt | more");
    : :
    : : If you want something embedded in quotes:
    : : system("cat "filename.txt" | more");
    : :
    : :
    : :
    : :
    : is there a way to do this with a variable?
    :

    yes
    [code]
    char command[126];
    strcpy(command,"cat "filename.txt" | more");
    system(command);
    [/code]

Sign In or Register to comment.