![]() |
|
#1
|
|||
|
|||
problem in system() function usageI 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:
CPP / C++ / C Code:
Last edited by cable_guy_67 : 09-Feb-2006 at 10:20.
Reason: Please enclose c code in [c] ... [/c] tags
|
|
#2
|
||||
|
||||
Re: problem in system() function usageBoth these programs work for me in Turbo C version 1
On Borland 5.5 only the CPP / C++ / C Code:
CPP / C++ / C Code:
You should also read the Guidelines, especially #1, #2, #4, #6 __________________
Age is unimportant -- except in cheese |
|
#3
|
|||
|
|||
Re: problem in system() function usageQuote:
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:
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
|
|||
|
|||
Re: problem in system() function usageactually, what you just said got me intrigued and I discovered:
On Windows XP with Command Extensions enabled, this works in the command prompt: Quote:
Update: have tested these programmatically: CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: problem in system() function usageQuote:
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
|
|||
|
|||
Re: problem in system() function usageI 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:
__________________
*Labor omnia consent*** Hardwork conquers all* |
|
#7
|
||||
|
||||
Re: problem in system() function usageQuote:
__________________
Age is unimportant -- except in cheese |
|
#8
|
|||
|
|||
Re: problem in system() function usageOh 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
|
|||
|
|||
Re: problem in system() function usageQuote:
Nothing like the portability of C programs, huh? Take Care. Rob! |
|
#10
|
|||
|
|||
Re: problem in system() function usageQuote:
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 GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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