Triggering files using C

CA50

Skilled
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
 
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"
 
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?
 
@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?
 
^ 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")
 
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;
	}
}
 
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,
 
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
 
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.
 
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"
 
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?
 
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.
 
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.
 
Back
Top