GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2006, 10:17
jaininaveen jaininaveen is offline
New Member
 
Join Date: Jan 2006
Posts: 25
jaininaveen is an unknown quantity at this point

problem in system() function usage


I am getting problem with system function when i run that program. immediatly i am comming out from tc and it says illigal operation. so please give solution for me
i am using windows xp
my program is
example:
CPP / C++ / C Code:
#include<stdlib.h>
#include<stdio.h>
#include<process.h>

int main(void)
{
   system("dir");
   return(0);    
}
example2:
CPP / C++ / C Code:
#include<stdlib.h>
#include<process.h>
#include<stdio.h>

int main(void)
{
      system("D:");
      system("mkdir naveen");
}
those both programs are not working inmy computer
Last edited by cable_guy_67 : 09-Feb-2006 at 10:20. Reason: Please enclose c code in [c] ... [/c] tags
  #2  
Old 09-Feb-2006, 10:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: problem in system() function usage


Both these programs work for me in Turbo C version 1
On Borland 5.5 only the
CPP / C++ / C Code:
system("D:");
doesn't work. But in if you combine them into one command it works:
CPP / C++ / C Code:
system("mkdir D:\\naveen");


You should also read the Guidelines, especially #1, #2, #4, #6
__________________

Age is unimportant -- except in cheese
  #3  
Old 09-Feb-2006, 18:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: problem in system() function usage


Quote:
Originally Posted by jaininaveen
I am getting problem with system function when i run that program. immediatly i am comming out from tc and it says illigal operation. so please give solution for me
i am using windows xp
my program is
example:
CPP / C++ / C Code:
#include<stdlib.h>
#include<stdio.h>
/*#include<process.h>  I deleted this */ /* <=== Dave's comment */

int main(void)
{
   system("dir");
   return(0);    
}

What compiler are you using? (And what is it that is in the header <process.h> that you need? I never include more than I need. The prototype for system() should be in <stdlib.h>.)

With Borland bcc32 (The free download version 5.5.1) and with Microsoft Visual Studio C++ and with cygwin/GNU gcc on my Windows XP box I get a listing of the directory from which I invoked the program.

That is: if my program is in c:\home\dave\test, and I open a command window and navigate to that directory, and I enter the program name, then I get a listing for that directory.

If you don't get a directory listing with your compiler, then you need another compiler (or you need to re-install that one). There is no need to go to the next step.

Quote:
Originally Posted by jaininaveen

example2:
CPP / C++ / C Code:
#include<stdlib.h>
/*#include<process.h>  I deleted this */ /* <=== Dave's comment */
#include<stdio.h>

int main(void)
{
      system("D:");
      system("mkdir naveen");
}

Now, we get into a situation where you simply can't do what I think you have in mind on Windows XP with system() calls.

The first system() call creates a process that performs as if you had entered the "D:" from the command line. (In other words changes current working directory to the D drive.) However the when process ends, control returns to your program, and nothing that happened in the process can affect the environment of the calling program (That is: anything like "cd" will not change the current working directory of the calling process). Dead end. You can try setting any kind of environment variables with system() calls, and you will find that they are not set (or changed) in the original working directory.

(Unlike the Linux bash shell, where multiple commands, separated by semicolons could be executed by a single system() call, I know of no way to string Windows commands together on a single line. If someone out there knows how, then I would appreciat hearing it, and we can all ignore my statements about "can't be done".)

Anyhow, in your example, the next system() call attempts to create a directory in the current directory (the one where you started).

Finally, no matter what happened in your various system() calls, you end up back in the directory where you started (This is assuming nothing drastic like a program crash or an operating system reboot)

If you want to something like this in a program, there are a couple of possibilities that come to mind:

Compilers (usually) have some implementation-dependent functions that allow you to change working directories, list directory contents, create new directories, etc. Since these are not part of any Standard C library, you may have to poke around your compiler's documentaton (or your installation's header files) to get some clues about such functions. For example, there might be a function named something like "chdir" for "change directory" (or, maybe not). The header file may be named something like <dir.h> or <direct.h> (or, maybe not).

There are a lot of Windows API functions that can be used from command line programs, and you can look around at msdn for them. I know that there are functions that let you navigate and inspect and create directories, etc., but I don't use them myself, so I can't be more specific. Maybe some other "helper" on this forum can chime in here with some good suggestions.

Windows batch files are not very well documented these days, and future versions of Windows may not have them, or they may change (drastically) what you can do and how you do it. But, it is possible to do almost anything in a batch file that you can do with a sequence of commands manually typed in a console window.

Your program could create a batch file (.bat) and then invoke that with a system() call. Or you could create the batch file separately (with a text editor) and feed the batch file's name to the program, if you needed to do something additional in the program that a batch file can't do. Lines in the batch file would make up the commands that you want to execute.

None of these methods is portable, but one of them might get you something useful to try.

Regards,

Dave
  #4  
Old 09-Feb-2006, 19:36
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: problem in system() function usage


actually, what you just said got me intrigued and I discovered:
On Windows XP with Command Extensions enabled, this works in the command prompt:
Quote:
Originally Posted by Command Prompt
C:\>cd windows & echo "two commands for the price of one!"
"two commands for the price of one!"

C:\WINDOWS>

Update: have tested these programmatically:
CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    system("cd c++");
    system("mkdir test_separate_calls");
    cout << "Did separate call test. Continue? (y/n) ";
    char cont;
    cin >> cont;
    cout << endl;
    if (cont == 'y')
    {
        system("cd c++ & mkdir test_together_call");
        cout << "Did together call test." << endl;   
    }
    cin.ignore();
    cin.get();
    return 0;
}
When run from the P:\ directory on my computer, it creates P:\test_separate_calls, and then after you enter 'y' at the prompt, creates P:\c++\test_together_call.
  #5  
Old 09-Feb-2006, 20:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: problem in system() function usage


Quote:
Originally Posted by ubergeek
actually, what you just said got me intrigued and I discovered:
On Windows XP with Command Extensions enabled, this works in the command prompt:

What can I say: It works for me, too.

Thanks a lot!!! One goodie more for my bag of tricks. I looked for something like this some time ago (a long time ago), and somehow just never found it.

Regards,

Dave
  #6  
Old 10-Feb-2006, 08:11
realnapster realnapster is offline
Junior Member
 
Join Date: Feb 2006
Posts: 51
realnapster will become famous soon enough

Re: problem in system() function usage


I have one more suggestion to make that i really very helpful while using the system() function instead of typing all commands and calling system() again and again what you really do is just make a batch file and include all of your commands in that save it and then call that batch file in your program this will increase the effeciency of your program as well as it will take you out of the trouble of calling system() again and again


example
c:\> copy con newbat.bat
date
time

now save this batch file by pressing ctrl+z and call it in your program as follows

CPP / C++ / C Code:
#include<stdlib.h>
#include<stdio.h>
int main()
{
   system("newbat.bat");
   return 0;
}
__________________
*Labor omnia consent*** Hardwork conquers all*
  #7  
Old 10-Feb-2006, 11:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: problem in system() function usage


Quote:
Originally Posted by realnapster
I have one more suggestion to make that i really very helpful while using the system() function instead of typing all commands and calling system() again and again what you really do is just make a batch file and include all of your commands in that save it and then call that batch file in your program this will increase the effeciency of your program as well as it will take you out of the trouble of calling system() again and again
realnapster, I have no idea what you native language is, but in English there are things called sentences. You end a sentence with a period. The next letter starts a new sentence and it is a capital letter. Other punctuation can be used but we'll start with this to help make your posts readable, please. Many of our members are not native English speakers and really need the help in making posts easy to read.
__________________

Age is unimportant -- except in cheese
  #8  
Old 10-Feb-2006, 12:57
realnapster realnapster is offline
Junior Member
 
Join Date: Feb 2006
Posts: 51
realnapster will become famous soon enough

Re: problem in system() function usage


Oh my god i always forgot all these things while posting my answers. Sir i will surely take care of all mistakes from now onwards. I will try to use simple english and take care of not using shortcuts of defining things. I really feel sorry for my english mistakes in my last posts.
__________________
*Labor omnia consent*** Hardwork conquers all*
  #9  
Old 10-Feb-2006, 13:30
rwehrli
 
Posts: n/a

Re: problem in system() function usage


Quote:
Originally Posted by realnapster
CPP / C++ / C Code:
#include<stdlib.h>
#include<stdio.h>
int main()
{
   system("newbat.bat");
   return 0;
}

Nothing like the portability of C programs, huh?


Take Care.

Rob!
  #10  
Old 10-Feb-2006, 13:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: problem in system() function usage


Quote:
Originally Posted by rwehrli
Nothing like the portability of C programs, huh?


Take Care.

Rob!

I think this has already been covered.

The system() function is part of the C standard library. But it's kind of obvious that use of the system() function (what's inside the parentheses) is necessarily system-dependent. Other solutions of the original problem (change to another directory on another disk drive and make a new directory) are also bound to be non-portable, since standard C and its libraries very deliberately are ignorant of file systems and other system physical properties.

Even something simple like listing directory contents requires some implementation-dependent functions, and solutions are, therefore, non-portable.

Regards,

Dave
Last edited by davekw7x : 10-Feb-2006 at 14:13.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 11:33
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 19:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.