Triggering files using C

CA50

Well-Known Member
Adept
Dec 26, 2009
3,385
65
87
GraveYard
Most of you have ever encountered with a MS-DOS batch file, where we can trigger an *.exe or any other file just by entering the file name. I want to know if this is also possible using C.:huh:

I hope i am clear to you. please reply
 

pr0ing

Well-Known Member
Adept
Sep 4, 2006
562
109
132
38
you can use the system() command from stdlib.h

Code:
system("run.exe");
will execute the run.exe file in the current directory.
 
  • Like
Reactions: 1 person

CA50

Well-Known Member
Adept
Dec 26, 2009
3,385
65
87
GraveYard
thanks man i will try it

@pr0ing
well i tried it, it worked for *.exe files but what about the txt or bat files. How to trigger them, when i used this command then, i threw an error as
"Bad file or command"
 

pr0ing

Well-Known Member
Adept
Sep 4, 2006
562
109
132
38
I am testing it on Windows XP using Visual Studio 6.

both these commands are working
Code:
system("test.txt");//opens the text file in the current directory
system("notepad.exe");
system() will take any DOS command as an input. So, if any command works at the DOS prompt then it should also work as a parameter to system().

"Bad file or command" usually occurs when the file/exe you are trying to acces is not in the current directory of the program which is running.
in the above code "notepad.exe" works as it is in windows/system32 folder.

what OS/Tools are you using?
 

CA50

Well-Known Member
Adept
Dec 26, 2009
3,385
65
87
GraveYard
@pr0ing
i am on winXP and i compile using Borland Turbo C

How come you compile C code through Visual Studio 6??
Does it support C?
 

kekerode

Active Member
Adept
Jul 29, 2009
570
7
32
Bangalore
^ Visual Studio 6 contains Visual C++ 6.0 ... so yes ... u can compile simple C code on that.

For Borland C ... try this

system("start test.txt")
 

CA50

Well-Known Member
Adept
Dec 26, 2009
3,385
65
87
GraveYard
Sory pal its not working. here is my code
Code:
#include<stdio.h>
#include<stdlib.h>
main()
{
	int a;
	printf("\n\nEnter your choice :\n\t1. Run Program\n\t2.Exit\n\n");
	scanf("%d",&a);
	if(a==1)
	{
		system("start 1.txt");
		getch();
		exit;
	}
	if(a==2)
	{
		exit;
	}
	else
	{
		exit;
	}
}
 

blufox

Well-Known Member
Adept
Jun 2, 2009
360
70
67
Mountain View, CA
CA50 said:
Most of you have ever encountered with a MS-DOS batch file, where we can trigger an *.exe or any other file just by entering the file name. I want to know if this is also possible using C.:huh:

I hope i am clear to you. please reply
Not possible in C.
IMNSHO, whoever says yes need to relearn C.

Cu,
 

CA50

Well-Known Member
Adept
Dec 26, 2009
3,385
65
87
GraveYard
Not possible in C.
IMNSHO, whoever says yes need to relearn C.
Sorry mate because this is possible as mentioned by pr0ing, system() function can to this, but it is limited to *exe fiiles. I can't trigger *txt/*bat or any other file types.

thanks for your concern
 

viridian

New Member
Disciple
Jun 26, 2008
231
16
0
11
ritesh.posterous.com
What do you mean trigger a txt file? Should it start the associated program with that extension? I can understand that exe files would run but what do you want the text files to do? . For txt files and everything else you'll probably need to system("start blah.txt") . This _should_ spawn notepad or whatever you have associated with txt files.
 

CA50

Well-Known Member
Adept
Dec 26, 2009
3,385
65
87
GraveYard
For txt files and everything else you'll probably need to system("start blah.txt") . This _should_ spawn notepad or whatever you have associated with txt files.
Thats what i want to do. but your method is not working."start blah.txt"
 

pr0ing

Well-Known Member
Adept
Sep 4, 2006
562
109
132
38
blufox said:
Not possible in C.
IMNSHO, whoever says yes need to relearn C.

Cu,
care to explain why the solution provided earlier is "not possible in C". I am open to learning new things.

or did you misinterpret the OP's question?
 

rock_ya_baby

~~~=o=~~~
Adept
Apr 1, 2005
362
61
117
Dark Side Of The Moon
www.cisco.com
You can simply do a conditional check for file extension which the user wants to launch and then run appropriate command to handle that.

Like:

if .txt

execute "notepad.exe filename.txt"

else if .jpg OR .gif OR .bmp

execute "mspaint.exe filename.jpg"

etc

Thats the best I can come up with at the moment ;)

CA50 said:
Thats what i want to do. but your method is not working."start blah.txt"
That will only work for executables.
 

CA50

Well-Known Member
Adept
Dec 26, 2009
3,385
65
87
GraveYard
if .txt
execute "notepad.exe filename.txt"
else if .jpg OR .gif OR .bmp
execute "mspaint.exe filename.jpg"
In this case i think that 'notepad.exe' and 'mspaint.exe' must be in that directory where the c-coded exe file will reside.