Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to run a command prompt command from within a c program. That command should take inputs from a text file or it should take those inputs from a for loop...I am m new to c programming..
eg:
To run a command color
This command color should take inputs from a text file ...text file contains inputs like a b c d each in a single line...
Posted
Updated 1-Apr-14 6:26am
v2
Comments
Rage 1-Apr-14 12:27pm    
Do you need to call the command from within the c-code, or do you want to create a C application that, once compiled, is a command that you can call from the command line ?
[no name] 1-Apr-14 19:29pm    
Why - what can a command line command do that can't be done in code? There are batch files for running scripts in the command interpreter. There may be some confusion here about what your actual requirement is.

Unfortunately you did not mention which OS you are running on.
I'll try with Windows anyway:

The function you are looking for is CreateProcess()
C++
BOOL WINAPI CreateProcess(
 TEXT("<path and filename of your application>"),
 TEXT("<CommandLine parameters>"),
...
);


You pass the name, path and command lines to your program to run.
The redirection of the console input and output is done by setting the flag STARTF_USESTDHANDLES in dwFlags and filling the appropriate members in the STARTUPINFO structure (underlined below).
C++
typedef struct _STARTUPINFO {
  DWORD  cb;
  LPTSTR lpReserved;
  LPTSTR lpDesktop;
  LPTSTR lpTitle;
  DWORD  dwX;
  DWORD  dwY;
  DWORD  dwXSize;
  DWORD  dwYSize;
  DWORD  dwXCountChars;
  DWORD  dwYCountChars;
  DWORD  dwFillAttribute;
  DWORD  dwFlags;
  WORD   wShowWindow;
  WORD   cbReserved2;
  LPBYTE lpReserved2;
  HANDLE hStdInput;
  HANDLE hStdOutput;
  HANDLE hStdError;
} STARTUPINFO, *LPSTARTUPINFO;


Here the links to the MSDN documentation for more details:
CreateProcess():
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx[^]

The STARTUPINFO structure:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx[^]

If this is not the intended OS, please let us know.

Good luck with your project!
Steve
 
Share this answer
 
v2
Comments
Member 10715106 2-Apr-14 1:16am    
Thank you for your reply...i dont know to use functions also im not good in programming...im using windows 7..i have a command which is used to unlock a file with a password..i have more than 100 passwords in a text file each line by line..my problem is that command should fetch each password at a time and execute it on command prompt to check whether it can unlock the file..can u please send me the complete program to do this operation..im using code blocks to run c,c++ programs...
You may want to ShellExecute "cmd.exe".
Here the doc[^]
 
Share this answer
 
Comments
Member 10715106 2-Apr-14 1:18am    
Thank you for your reply...i dont know to use functions also im not good in programming...im using windows 7..i have a command which is used to unlock a file with a password..i have more than 100 passwords in a text file each line by line..my problem is that command should fetch each password at a time and execute it on command prompt to check whether it can unlock the file..can u please send me the complete program to do this operation..im using code blocks to run c,c++ programs...
This will work on Posix systems (including Windows).

Example:

C++
#include <stdlib.h>

system("cmd.exe");
system("bash");
</stdlib.h>


http://pubs.opengroup.org/onlinepubs/009695399/functions/system.html[^]
 
Share this answer
 
Invoke the command processor using system()


C++
#include <conio.h>
#include <string>

using namespace std;

void main(void)
{
	string strcommand = "color "; // Command
	string strcolor = "5B";       // Parameter - read from text file.

	system( (strcommand + strcolor).c_str() );

	getch();
}
 
Share this answer
 
v2
Comments
Member 10715106 2-Apr-14 1:18am    
Thank you for your reply...i dont know to use functions also im not good in programming...im using windows 7..i have a command which is used to unlock a file with a password..i have more than 100 passwords in a text file each line by line..my problem is that command should fetch each password at a time and execute it on command prompt to check whether it can unlock the file..can u please send me the complete program to do this operation..im using code blocks to run c,c++ programs...
[no name] 2-Apr-14 1:37am    
Sorry - not how it works here. You must take the next step.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900