Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 14th, 2005, 10:55 PM   #1
Da-Kid
Programmer
 
Join Date: Nov 2004
Posts: 47
Rep Power: 0 Da-Kid is on a distinguished road
System Info

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

        system( "echo Date: `date`" );
        system( "echo Uptime: `uptime`" );
        system( "echo Name: `uname -n`" );
        system( "echo Kernel: `uname -r`" );
}
I run this program with the command ./sysinfo
But I want to be able to add flags for instance ./sysinfo -d prints only the date line, can someone tell me how to make that possible? And also I found a line in bash that displays the hdd space.
 df | awk '{ sum+=\$2/1024^2 }; END { printf (\"%dGb\", sum 
             )}
Can someone tell me how to turn that into C++ code?
Da-Kid is offline   Reply With Quote
Old Feb 14th, 2005, 11:04 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
You're going to need to declare main like this:

 int main(int argc, char *argv[])

where argc is the number of arguments that are passed from the command line, and argv is a string representation of an argument. So for instance, in your -d example, argc would be 2 (I believe), since their are two arguments (the name of your program and "-d"), and argv[1] would be "-d".
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 14th, 2005, 11:19 PM   #3
Da-Kid
Programmer
 
Join Date: Nov 2004
Posts: 47
Rep Power: 0 Da-Kid is on a distinguished road
#include <iostream>
#include <cstdlib>

using namespace std;
         
int main(int argc, char *argv[2])
{        
        
        system( "echo Date: `date`" );
        system( "echo Uptime: `uptime`" );
        system( "echo Name: `uname -n`" );
        system( "echo Kernel: `uname -r`" );
}
So with that code above, if I run ./sysinfo -d it'll only print the Date line?
Da-Kid is offline   Reply With Quote
Old Feb 14th, 2005, 11:45 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
You would need some if statements.

And main would have to be declared as such:

int main(int argc, char *argv[])

And then, if you want to use the -d flag or something you could do something like this:

 if(strcmp(argv[2], "-d") == 0)
 {
 	 // print out date
 }

Keep in mind, strcmp() is in the string.h header file.

Also, just for your info, you could also declare main like this:

int main(int argc, char **argv)

but I've never done it like that. I always use the method I posted before. Also, the argument names could be changed to best suit your needs, argc and argv are just customary, though.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 15th, 2005, 12:28 AM   #5
Da-Kid
Programmer
 
Join Date: Nov 2004
Posts: 47
Rep Power: 0 Da-Kid is on a distinguished road
You kinda lost me, but I tried the code
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

        system( "echo Date: `date`" );
        system( "echo Uptime: `uptime`" );
        system( "echo Name: `uname -n`" );
        system( "echo Kernel: `uname -r`" );

        if(strcmp(argv[2], "-d") == 0)
        {
                system( "echo Date: `date`" );
        }
}
And as a result I got
c0debox programming # ./sysinfo -d
Date: Tue Feb 15 00:26:13 EST 2005
Uptime: 00:26:13 up 2:33, 4 users, load average: 0.07, 0.08, 0.06
Name: c0debox
Kernel: 2.6.10
Segmentation fault
Da-Kid is offline   Reply With Quote
Old Feb 15th, 2005, 12:43 AM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I appologize. It should be argv[1]. I don't know what I was thinking.

Anyway, here's code for you:

  #include <iostream>
  #include <string.h>
  #include <stdio.h>
  
  using namespace std;
  
  int main(int argc, char *argv[])
  {	
  	if(argc>1)
  	{
  		if(strcmp(argv[1], "-d") == 0)
  		{
      			    system( "echo Date: `date`" );
  		}
  	}		
  	else
  	{
  		system( "echo Date: `date`" );	
  		system( "echo Uptime: `uptime`" );
  		system( "echo Name: `uname -n`" );
  		 system( "echo Kernel: `uname -r`" );
  	}
  
  		
  }
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 15th, 2005, 1:22 AM   #7
Da-Kid
Programmer
 
Join Date: Nov 2004
Posts: 47
Rep Power: 0 Da-Kid is on a distinguished road
Cool it worked. Now can you explain this line for me so I can set the flags up for everything else
  	if(argc>1)
  	{
  		if(strcmp(argv[1], "-d") == 0)
  		{
      			    system( "echo Date: `date`" );
  		}
Da-Kid is offline   Reply With Quote
Old Feb 15th, 2005, 5:57 AM   #8
Da-Kid
Programmer
 
Join Date: Nov 2004
Posts: 47
Rep Power: 0 Da-Kid is on a distinguished road
// Written by c0demuncher

#include <iostream>
#include <string>

using namespace std;

void c0de()
{
        system( "echo Date: `date`" );
        system( "echo Uptime: `uptime`" );
        system( "echo Name: `uname -n`" );
        system( "echo CPU: `uname -p`" );
        system( "echo Kernel: `uname -r`" );
}

int main(int argc, char *argv[])
{
        if(argc>1)
        {
                if(strcmp(argv[1], "-d") == 0)
                {
                        system( "echo Date: `date`" );
                }
        }
        else
        {
                 c0de();
        }
}
Thats my updated code, but I'm not sure where to add that if statement to add more flags, and what exactly that if statement means
Da-Kid is offline   Reply With Quote
Old Feb 15th, 2005, 7:00 AM   #9
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
To be honest with you, you need to go back to the fundementals of programming looking at if statements, string handlers loops etc etc before you get into this because if you had read about arrays and if statements this would be a breeze for you.
Berto is offline   Reply With Quote
Old Feb 15th, 2005, 7:46 AM   #10
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
if(argc>1) means if there's more than one argument passed from main. Everytime you leave a space when calling a program, and then add another string after the name of the program, you're passing arguments to it. The first argument is the name of the program, the second argument is, in this case, "-d." argc just counts arguments.

if(strcmp(argv[1], "-d") == 0)

means if argv[1] (argument passed from main) is -d then you do whatever code that follows.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC