LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-12-2010, 11:07 PM   #1
LazerPhreak
Member
 
Registered: Dec 2009
Location: USA
Distribution: Ubuntu Server / Desktop / UNR
Posts: 117

Rep: Reputation: 17
How to include variables in system() calls


Hello all!

I need to include variables in a system() call.
This is that I have:

[code]#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

short sect1, sect2, sect3, sect4;

int main()
{
cout << "Enter IP Address: ";
cin >> sect1;
system("ping -c 1 PUT_SECT1_VARIABLE_HERE");
return 0;
}

How can I do this?

P.S.: I know this is a pointless redundant program, but it's part of a larger project learning process.

Thanks
 
Old 03-12-2010, 11:28 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
This appears to be a C++ program. Therefore get rid of the includes for "stdio.h" and "stdlib.h". In C++, when including "C headers", we usually prepend it with "c" and remove the ".h". So, "stdlib.h" would become "cstdlib", so include that instead.

Code:
system("ping -c 1 PUT_SECT1_VARIABLE_HERE");
System just expects a "char*". Create a C++ string object, dynamically, and pass its "c_str" to "system". For example
Code:
int foo = 5;
string bar = "text and " + foo;
system(bar.c_str());
 
Old 03-12-2010, 11:39 PM   #3
LazerPhreak
Member
 
Registered: Dec 2009
Location: USA
Distribution: Ubuntu Server / Desktop / UNR
Posts: 117

Original Poster
Rep: Reputation: 17
OK so here I am:
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

short sect1, sect2, sect3, sect4;

int main()
{

	cout << "Enter part 1: ";
	cin >> sect1;
	string ip_addr = "ping -c 1 " + sect1;
	system(ip_addr.c_str());
return 0;
}
I'm getting an error though:
Code:
sh: Syntax error: Unterminated quoted string
I cant seem to find any missing quotes though...

Last edited by LazerPhreak; 03-12-2010 at 11:43 PM.
 
Old 03-13-2010, 01:21 AM   #4
LazerPhreak
Member
 
Registered: Dec 2009
Location: USA
Distribution: Ubuntu Server / Desktop / UNR
Posts: 117

Original Poster
Rep: Reputation: 17
So I dont get it....when i enter 123 for sect1 i get the error message I previously said, but when I enter abc I get an empty line as output. Any ideas?
 
Old 03-13-2010, 01:37 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
How 'bout trying to print the string first:
Code:
puts(ip_addr.c_str());
putchar('\n');
or
Code:
cout << ip_addr.c_str() << '\n';
This is so that you can know the command you're trying to execute.

Last edited by konsolebox; 03-13-2010 at 01:38 AM.
 
Old 03-13-2010, 11:20 AM   #6
LazerPhreak
Member
 
Registered: Dec 2009
Location: USA
Distribution: Ubuntu Server / Desktop / UNR
Posts: 117

Original Poster
Rep: Reputation: 17
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>

using namespace std;

int sect1;

int main()
{

	cout << "Enter part 1: ";
	cin >> sect1;
	string ipaddr = "echo " + sect1;
	cout << ipaddr.c_str() << '\n';
	//system(ipaddr.c_str());
return 0;
}
Code:
$ ./a.out
Enter part 1: 123
U	�	�"
$ ./a.out
Enter part 1: abc
echo 
$
That explains the problem, but How the hell did that get created as my output of:
Code:
string ipaddr = "echo " + sect1;
cout << ipaddr.c_str() << '\n';

Last edited by LazerPhreak; 03-13-2010 at 11:34 AM.
 
Old 03-13-2010, 03:19 PM   #7
LazerPhreak
Member
 
Registered: Dec 2009
Location: USA
Distribution: Ubuntu Server / Desktop / UNR
Posts: 117

Original Poster
Rep: Reputation: 17
So I've been looking around, but I can't find any solution. Any help?
 
Old 03-13-2010, 03:49 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
User input in a system() is a Very Bad Idea. Use fork+exec so that odd input won't do very bad things, for example, "127.0.0.1 & rm -rf /" will cause the program to attempt to delete all files on the system.
 
0 members found this post helpful.
Old 03-13-2010, 04:02 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by LazerPhreak View Post
So I've been looking around, but I can't find any solution. Any help?
Consider using the old fashioned "C" way with 'sprintf':

man 3 sprintf
.

I.e. first construct you command line using 'sprintf' and then use it.
 
1 members found this post helpful.
Old 03-13-2010, 06:01 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Use
Code:
string ipaddr = string("echo ") + sect1;
The problem is that "echo " evaluates to a char* so when you add the integer to it you get a pointer to some random place in memory. Just one of those annoying C++ quirks.
 
1 members found this post helpful.
Old 03-13-2010, 06:43 PM   #11
LazerPhreak
Member
 
Registered: Dec 2009
Location: USA
Distribution: Ubuntu Server / Desktop / UNR
Posts: 117

Original Poster
Rep: Reputation: 17
So I got it working, thanks to a combination of your support and google. This is what I have:

Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main()
{
    cout << endl << "Ping : ";
    string sect1;
    cin >> sect1;
    string ipaddr = string("ping ").append(ping);
    system(ipaddr.c_str());
}
 
  


Reply



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
how does java calls the system calls which are written in c babu198649 Linux - General 3 12-05-2011 03:40 AM
ms access like calls (ie openrecordset) to Mysql in c program include database,h pjlinux100 Programming 9 02-10-2008 04:45 PM
How do I include my own system call in file system driver kgp Linux - Kernel 3 06-06-2007 10:41 AM
Passing variables to an include? Red Squirrel Linux - Software 4 09-23-2005 09:18 PM
Variable names that include variables.... mychl Programming 7 09-24-2003 07:13 AM

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

All times are GMT -5. The time now is 12:21 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