LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2007, 05:57 AM   #1
sudif
LQ Newbie
 
Registered: Sep 2005
Posts: 11

Rep: Reputation: 0
system(variable) is it possible??


i want to make a c++ program that do something like this
string str;
cin >> str;
str="cat " + str;
int rv=system(str);

is there a way to do it?
 
Old 04-01-2007, 06:14 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It should be fine, the only thing to watch out for is that you will probably need to convert the string to a character array check out the method c_str().
 
Old 04-01-2007, 10:40 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Also if the first arg in string addition is a string const you will need explicit construction:
Code:
string str;
cin >> str;
str=string("cat ") + str;
int rv=system(str.c_str());
The >> operator will only give you a section of non-whitespace, so I'd use getline instead in case someone wants to arrive at the filename with ``, or it has spaces and requires "". That involves resizing the string in advance, though:
Code:
str.resize(512);
cin.getline(&str[0], 512);
--or--
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
  char input[512];
  cin.getline(input, 512);
  string command = string("cat ") + input;
  return system(command.c_str());
}
Save that as 'test.cpp', and after compiling run it 3 times using each of these as input:
Code:
test.cpp
`ls test.cpp`
"test.cpp"
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
setting up system variable delmoras Linux - Newbie 5 03-02-2006 07:56 AM
System Path Enviroment Variable innovations Linux From Scratch 2 10-26-2005 04:27 PM
same system variable, but with different contents? kpachopoulos Linux - General 1 07-31-2005 05:15 PM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 12:40 PM
Where to set system PATH variable Cybers19 Linux - Newbie 4 05-05-2004 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration