C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-17-2005, 04:55 PM   #1
Registered User
 
Join Date: Nov 2005
Posts: 36
Question about the System() function...

I am writing a program that acts like a shell. I used the System() function to compute the commands, however, I realized that exit and cd do not work. I figured out how to make my own exit, however, I cannot figure out how to get cd to work...if anyone could help that'd be great, I appreciate it in advance....here's my code...

Code:
#include <iostream>
#include <stdlib.h>
#include <string>


void main()
{
        char cmd[100] = " ";

        cout << "Please enter command: ";
        cin.getline(cmd, 100);

        while (strcmp (cmd, "exit") !=0)
                {

                        system (cmd);
                        cout << "Please enter command: ";
                        cin.getline(cmd,100);
                }
                exit(1);
}
YankeePride13 is offline   Reply With Quote
Old 11-17-2005, 06:08 PM   #2
Registered User
 
Join Date: Nov 2005
Posts: 36
this is the updated code...

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <unistd.h>

void main()
{
        char cmd[100] = " ";
        char * pch;

        cout << "Please enter command: ";
        cin.getline(cmd, 100);

        while (strcmp (cmd, "exit") !=0)
        {
                if((cmd[0] == "c") && (cmd[1] == "d"))
                {
                        pch = strtok(cmd," ");
                        pch = strtok(NULL," ");
                        chdir(pch);
                }
                else
                {
                        system (cmd);
                        cout << "Please enter command: ";
                        cin.getline(cmd,100);
                }
        }
        exit(1);
}
and there are the errors im recieving from my linux compiler

test.cpp:16: ISO C++ forbids comparison between pointer and integer
test.cpp:16: ISO C++ forbids comparison between pointer and integer

any ideas?
YankeePride13 is offline   Reply With Quote
Old 11-18-2005, 12:45 PM   #3
Me
 
-=SoKrA=-'s Avatar
 
Join Date: Oct 2002
Location: Europe
Posts: 448
You're getting those errors because you're comparing a string (char pointer) and an int.

You should change the line that says
Code:
if((cmd[0] == "c") && (cmd[1] == "d")
to
Code:
if((cmd[0] == 'd') && (cmd[1] == 'd')
and there you go. No more warning, and a working piece of code.

Also, please change the return value of main to int. Void is just wrong (see the FAQ or search the forums for the reason(s)).

It might be better to fork() and then exec(), but that's a bit more complicated.
__________________
SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
I say what I say, I mean what I mean.
IDE: emacs + make + gcc and proud of it.
-=SoKrA=- is offline   Reply With Quote
Old 11-18-2005, 02:41 PM   #4
Registered User
 
Join Date: Sep 2004
Posts: 197
Im wondering why you wouldn't be getting errors with this, since cin and cout are in the std namespace, and I don't see a "using namespace std;" or any thing like that in your code. Also, its int main, not void.
__________________
If any part of my post is incorrect, please correct me.

This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.
Xipher is offline   Reply With Quote
Old 11-18-2005, 02:45 PM   #5
Me
 
-=SoKrA=-'s Avatar
 
Join Date: Oct 2002
Location: Europe
Posts: 448
He's probably using some very permissive compiler. My g++ erros out on void main().

Also, I suspect this is not the code verbatim, because the line where he's got the error and it's actual line in the code he posted are different.
__________________
SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
I say what I say, I mean what I mean.
IDE: emacs + make + gcc and proud of it.
-=SoKrA=- is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
question abt function system(); errtime C Programming 2 10-01-2007 01:24 PM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
measuring system resources used by a function Aran C Programming 1 03-13-2006 05:35 PM
Including lib in a lib bibiteinfo C++ Programming 0 02-07-2006 02:28 PM
[question]Simulating the Reliability of a Component-Based System burbose C Programming 3 06-13-2005 07:28 AM


All times are GMT -6. The time now is 01:59 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22