Hi guys..

I'm still going through C++, learning new stuffs. It is true that I have finished my C++ course two years ago. But, you all know C++ have too much things to learn!

Anyway, I need help guys "kindly" how can I open a program/or any file using C++?

What I want is when I "Compile, Build and Run" the C++ code, it'll open for me a program that I specified in the code.

Can anyone explains to me how to do that? Does have to do with input/output file stream?

I found this code, which makes you type the name of the program and it opens it.
Can you kindly explains it to me too?

#include <iostream>
#include <conio.h>
using namespace std;
int main() {
char s1[256],s2[256];
cout<<"Program name: ";
gets(s1);
sprintf(s2,"START %s",s1);
const char* prgm=s2;
system(prgm);
}

Thanks all. :)

system(prgm);

That actually calls the other program, pretty much everything else is just fluff.

I'm afraid I don't know another way because I don't know Win32 API or any other APIs. I do know that this is a very insecure method though.

> It is true that I have finished my C++ course two years ago.
And it looks like you forgot everything since.

> gets(s1);
This is just AWFUL

> sprintf(s2,"START %s",s1);
So what happened to using std::string (since this is C++ - right?)

> const char* prgm=s2;
And this does what exactly, that system(s2) can't?

Not to mention, it's only 5 lines long and already the lack of indentation is making it look messy.

commented: Thank you man. +2

@Fbody: Umm I don't know.. I just want to understand how this code works! And I need a code, or how to write a code that open a program specifically that you specify in the code, when u run it.

@Salem: Come on man, you make me feel like I've never studied C++ - LOL!
I know those things, about "get" which is used for getting string, it works like "cin" and we can use "getline"..etc

The problem is what is "prgm"? can it be changed? is it a variable? or it is constant? I mean or it is fixed?

Also, "sprintf" this is my first time seeing it! I really studied C++, but all basics not for advanced users.

Thanks anyway for ur time mates.

@Fbody: Umm I don't know.. I just want to understand how this code works! And I need a code, or how to write a code that open a program specifically that you specify in the code, when u run it.

@Salem: Come on man, you make me feel like I've never studied C++ - LOL!
I know those things, about "get" which is used for getting string, it works like "cin" and we can use "getline"..etc

The problem is what is "prgm"? can it be changed? is it a variable? or it is constant? I mean or it is fixed?

Also, "sprintf" this is my first time seeing it! I really studied C++, but all basics not for advanced users.

Thanks anyway for ur time mates.

...I just want to understand how this code works!...

char s1[256],s2[256]; //creates 2 char arrays
cout<<"Program name: "; //prompts user for name of program
gets(s1); //gets the user's input
sprintf(s2,"START %s",s1); //prepends "START " to user input string s1, stores result in s2
const char* prgm=s2; //wastes compute cycles and memory by making an unnecessary copy of s2
system(prgm);  //sends command to system to start program and invites security issues
commented: Thank you man. +2
char s1[256],s2[256]; //creates 2 char arrays
cout<<"Program name: "; //prompts user for name of program
gets(s1); //gets the user's input
sprintf(s2,"START %s",s1); //prepends "START " to user input string s1, stores result in s2
const char* prgm=s2; //wastes compute cycles and memory by making an unnecessary copy of s2
system(prgm);  //sends command to system to start program and invites security issues

WOW. Thank you SO MUCH!
This really helped me a lot.

Can I change this:

sprintf(s2,"START %s",s1); //prepends "START " to user input string s1, stores result in s2
const char* prgm=s2; //wastes compute cycles and memory by making an unnecessary copy of s2

To this:

const char* prgm = s1;

??????


Thank you man, really appreciated.

Can I change this:

sprintf(s2,"START %s",s1); //prepends "START " to user input string s1, stores result in s2
const char* prgm=s2; //wastes compute cycles and memory by making an unnecessary copy of s2

To this:

const char* prgm = s1;

??????

No.

You need the sprintf() to set up the command correctly. Please re-read Salem's post.

No.

You need the sprintf() to set up the command correctly. Please re-read Salem's post.

I guess no. It doesn't have to be used.

See, I did some modification from what I understood to the code, and YEAH IT WORKED HAHAHAHAHA:

#include <iostream>
#include <conio.h>

using namespace std;


int main()
{

	char s1[256] = "START notepad";
	
	const char* prgm = s1;
	
	system( prgm );

	return 0;
}

Now, this code will open "notepad" directly when you execute the code :D

Thanks a lot guys. I found what I'm looking for. ^^

Gr8.

I was under the impression you wanted to get the name of the executable from the user. But, if you want it hardcoded, that would definitely work. In fact, you could simply do:

#include <iostream>
#include <conio.h>

using namespace std;


int main()
{
	
	system( "START notepad" );

	return 0;
}

Gr8.

I was under the impression you wanted to get the name of the executable from the user. But, if you want it hardcoded, that would definitely work. In fact, you could simply do:

#include <iostream>
#include <conio.h>

using namespace std;


int main()
{
	
	system( "START notepad" );

	return 0;
}

This is cooler mate :)
I'm gonna use yous ^^
Thanks.

Very Clear Now. I understand this :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.