Hi everyone! Does anyone here know how to create a C program that will open a web browser depending on the user input? For example if the user inputs the integer 1, the web browser will be opened and will go to 'http://www.daniweb.com', when the input is 2, 'http://www.google.com' will be opened, and so on. The websites that will be opened will be pre-determined, meaning it will be set in the program source code that everytime the input is 1 the web browser will go to 'http://www.daniweb.com' only; no random choosing of websites will be done.

Replies and/or links to forums, tutorials, sample codes, or to other pertinent threads are greatly appreciated. Thanks!

Calling an external program is actually completely dependent to the operating system that is running the program. I know some methods in Linux to call for a program to run it.

So, one easy but not recommended way is to use the notorious "system()" function in C. If you are running windows, say:

system("C:\\program files\\Mozilla\\Firefox.exe http://www.yahoo.com"); //This is an example, I don't know the exact address

or if you have added it to your path, you can simply say:

system("firefox http://www.yahoo.com");

Just try to provide more information about what you need this program for, to think about the alternatives.

Calling an external program is actually completely dependent to the operating system that is running the program. I know some methods in Linux to call for a program to run it.

So, one easy but not recommended way is to use the notorious "system()" function in C. If you are running windows, say:

system("C:\\program files\\Mozilla\\Firefox.exe http://www.yahoo.com"); //This is an example, I don't know the exact address

or if you have added it to your path, you can simply say:

system("firefox http://www.yahoo.com");

Just try to provide more information about what you need this program for, to think about the alternatives.

I see. But I'll be creating and running the program in Wubi, so I think it's considered a different OS right? May I know what are the ways to do it in Wubi/Linux? Thank you.

That's what I like, Linux!!! hehehe... take a look at the code below:

#include <unistd.h>
#include <stdio.h>

int main(void)
{
	char* prog[3];
	prog[0] = "firefox";
	prog[1] = "http://www.yahoo.com";
	prog[2] = '\0';

	execvp(prog[0], prog);
}

Oh, I used "stdio.h" library to run some tests. You don't need it for running the above code!!!

That's what I like, Linux!!! hehehe... take a look at the code below:

#include <unistd.h>
#include <stdio.h>

int main(void)
{
	char* prog[3];
	prog[0] = "firefox";
	prog[1] = "http://www.yahoo.com";
	prog[2] = '\0';

	execvp(prog[0], prog);
}

Thanks a lot man! This is a real big help. I tried to tweak it a little bit to see how it works, and I learned that I can't open a new tab/window when I already opened one. Now may I ask for some ways to make the program open new tabs/windows after the browser has opened a website? I'm thinking on implementing timers such that when a website is opened and after a couple of seconds another website will be opened. Could that be made possible?

May not be the best way to do it, by just give it a try:

#include <unistd.h>

int main(void)
{
	char *prog[3];
	pid_t pid = fork();
	if (pid == 0) //Child process
	{
		printf("Child running...\n");
		prog[0] = "firefox";
		prog[1] = "http://www.yahoo.com";
		prog[2] = '\0';
		execvp(prog[0], prog);
	}
	else
	{
		int i, j;
		printf("Parent running...\n");
		prog[0] = "firefox";
		prog[1] = "http://www.google.com";
		prog[2] = '\0';
		//You need to wait???
		for (i = 0; i < 1000000; i++) //Waiting
			for (j = 0; j < 10000; j++);
		execvp(prog[0], prog);
	}
}

May not be the best way to do it, by just give it a try:

#include <unistd.h>

int main(void)
{
	char *prog[3];
	pid_t pid = fork();
	if (pid == 0) //Child process
	{
		printf("Child running...\n");
		prog[0] = "firefox";
		prog[1] = "http://www.yahoo.com";
		prog[2] = '\0';
		execvp(prog[0], prog);
	}
	else
	{
		int i, j;
		printf("Parent running...\n");
		prog[0] = "firefox";
		prog[1] = "http://www.google.com";
		prog[2] = '\0';
		//You need to wait???
		for (i = 0; i < 1000000; i++) //Waiting
			for (j = 0; j < 10000; j++);
		execvp(prog[0], prog);
	}
}

Thanks a lot again! The program worked really well, I just substituted sleep() for the 2 forloops though. Now I'm just curious if it's possible to open 4 websites successively? I tried to read the manual for the function fork() but it seems like the parent process can only create 1 new child process. Or can it spawn another 2 child processes to make it 4 processes total (1 parent, 3 children)? I need to open 4 websites because I'm going to use the program to control a surveillance camera to face into a specific point in space. My program looks like

if (pid == 0) { //Child process
      prog[0] = "firefox";
      prog[1] = "http://Camera_IP_address/decoder_control.cgi?command=98"; //tilt up
      prog[2] = '\0';

      execvp(prog[0],prog);
}

else { //Parent process
      prog[0] = "firefox";
      prog[1] = "http://Camera_IP_address/decoder_control.cgi?command=99"; //stop tilt up
      prog[2] = '\0';

      sleep(3); //pause for 3 seconds
      execvp(prog[0],prog);      
}

The code above isn't enough because I also need to command the camera to pan right and after a few seconds stop it. Which means I still need to open 2 more websites. How could the program be edited for this purpose? Thanks!

May not be the best way to do it, by just give it a try:

#include <unistd.h>

int main(void)
{
	char *prog[3];
	pid_t pid = fork();
	if (pid == 0) //Child process
	{
		printf("Child running...\n");
		prog[0] = "firefox";
		prog[1] = "http://www.yahoo.com";
		prog[2] = '\0';
		execvp(prog[0], prog);
	}
	else
	{
		int i, j;
		printf("Parent running...\n");
		prog[0] = "firefox";
		prog[1] = "http://www.google.com";
		prog[2] = '\0';
		//You need to wait???
		for (i = 0; i < 1000000; i++) //Waiting
			for (j = 0; j < 10000; j++);
		execvp(prog[0], prog);
	}
}

Hey, do you know how to create multiple threads to open multiple websites one after another? I'm trying pthread right now and will see later if it works. Thank you

Yeap, exactly, that's why if you look at my post, I mentioned that:

May not be the best way to do it, by just give it a try:

The reason is very simple, creating a new process means wasting CPU time and memory space while threads are meant to be for this purpose. So please do not expect me to write the code for threads, but I can suggest you initially to understand the real meaning of process, thread(lightweight process) and why or where we used them.

Then definitely you can go to pthreads library available for C. Let me give you the heads up, playing with threads is a headache, be ready for some challenge, but once you got the idea, it would be piece of cake.

Write your code and if you needed any help, I suggest you to start a new thread and we will help you.

Good luck!!! ;)

Yeap, exactly, that's why if you look at my post, I mentioned that:


The reason is very simple, creating a new process means wasting CPU time and memory space while threads are meant to be for this purpose. So please do not expect me to write the code for threads, but I can suggest you initially to understand the real meaning of process, thread(lightweight process) and why or where we used them.

Then definitely you can go to pthreads library available for C. Let me give you the heads up, playing with threads is a headache, be ready for some challenge, but once you got the idea, it would be piece of cake.

Write your code and if you needed any help, I suggest you to start a new thread and we will help you.

Good luck!!! ;)

So is it really possible to implement this using threads? I mean, in one of the tutorials I've read, I learned that one way to terminate a thread is to call the exec subroutine (and in my case I'm calling execvp). Or is there a way to implement this using threads and I just need to figure that out?

Yeap, exactly, that's why if you look at my post, I mentioned that:


The reason is very simple, creating a new process means wasting CPU time and memory space while threads are meant to be for this purpose. So please do not expect me to write the code for threads, but I can suggest you initially to understand the real meaning of process, thread(lightweight process) and why or where we used them.

Then definitely you can go to pthreads library available for C. Let me give you the heads up, playing with threads is a headache, be ready for some challenge, but once you got the idea, it would be piece of cake.

Write your code and if you needed any help, I suggest you to start a new thread and we will help you.

Good luck!!! ;)

I created a new thread where I posted a part of my newly created code implementing pthread. Hope you could take a look at it here: http://www.daniweb.com/forums/thread326389.html . Thanks man

use a fork() inside the child process along with sleep() calls

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.