By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

function taking file as an argument and open that file in browser

P: 23
Hi,

Please help me.

I want to create a function taking file as an argument and open that file in the browser.

wfstream file_stream;

void function(string filepath)
{
cout<<"Open The file"<<endl;
file_stream.open( filepath.c_str() , ios::in );

if ( file_stream.is_open() )
{
cout<<"Open Successfully"<<endl;
cout<<"Try to open in browser"<<endl;

How to implemment in c++ code

}
else
{
cout<<"Unable to open the file"<<endl;
}
}

please give me some information how to ipmlement those things, please reply me some one quickly.

Thanks in Advance.
Nov 5 '08 #1
Share this Question
Share on Google+
18 Replies


100+
P: 256
I would use system( "whatbrowseryoulike whatyourfileiscalled"); which is probably going to do the job, but isn't the most elegant solution.

Also, you probably don't need to open the file yourself, just let the browser have at it.
Nov 5 '08 #2

100+
P: 424
A slightly more elegant way would be to determine the default viewer for the file extension from registry. For .HTM files, for example, [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.htm]
@="FirefoxHTML"

Then go to the FirefoxHTML key:

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\FirefoxHTML\sh ell\open\command]
@="C:\\PROGRA~1\\MOZILL~1\\FIREFOX.EXE -url \"%1\""

Now you will have the path and executable of the browser and even the correct syntax to open your file. All this can be accomplished in your program using windows registry functions and will work for all files, not just .htm files.
Nov 5 '08 #3

P: 28
A slightly more elegant way would be to determine the default viewer for the file extension from registry..
No.
Never use the registry.
Simply use OS api (Win32 api if Windows)
Nov 5 '08 #4

P: 12
Take a look at this
http://www.dreamincode.net/code/snippet357.htm

suppose you have on Windows a file called openBrowser like this:
#include <windows.h>

void main()
{
ShellExecute(NULL, "open", "http://dreamincode.net", NULL, NULL, SW_SHOWNORMAL);
}

compile it by:
cl /EHsc openBrowser.cpp /link shell32.lib

ch33rs,
lini
Nov 5 '08 #5

100+
P: 424
No.
Never use the registry.
Simply use OS api (Win32 api if Windows)
Do you have a better way of finding out the default browser in Windows? If so, I'd like to hear about it. And the registry commands I mentioned are actually part of the Win32 API.
Nov 6 '08 #6

100+
P: 424
ShellExecute(NULL, "open", "http://dreamincode.net", NULL, NULL,SW_SHOWNORMAL);
Yes, that works for a URL, but try it with a file:

ShellExecute(NULL, "open", "file:///C:/Users/test.txt", NULL,NULL,SW_SHOWNORMAL);

will open the file in the default viewer, not the web browser.
Nov 6 '08 #7

P: 23
Take a look at this
http://www.dreamincode.net/code/snippet357.htm

suppose you have on Windows a file called openBrowser like this:
#include <windows.h>

void main()
{
ShellExecute(NULL, "open", "http://dreamincode.net", NULL, NULL, SW_SHOWNORMAL);
}

compile it by:
cl /EHsc openBrowser.cpp /link shell32.lib

ch33rs,
lini



Hi Lini,

this is working for windows, but i want make work in linux. when i try to compile with g++

it shows me two errors that
error: ‘SW_SHOWNORMAL’ was not declared in this scope
error: ‘ShellExecute’ was not declared in this scope

i have includede th windows.h but why these two error in linux.

Please reply me. i wait ur reply.

Thanks,
Mamul
Nov 6 '08 #8

P: 23
I would use system( "whatbrowseryoulike whatyourfileiscalled"); which is probably going to do the job, but isn't the most elegant solution.

Also, you probably don't need to open the file yourself, just let the browser have at it.

Hi,
I have tried with ur code like

#include"stdlib.h"

system("http://www.google.co.in/");

but showing me the output that----

' http: ' is not recognized as an internal and external command.
so please tell me wt is the problem.

Thanks.
Nov 6 '08 #9

P: 23
Hi All,

#include"windows.h'
ShellExecute(NULL, "open", filepath.c_str(),NULL, NULL, SW_SHOWNORMAL);

this is working for windows, but i want make work in linux. when i try to compile with g++

it shows me two errors that

windows.h : there is no such file or directory
error: ‘SW_SHOWNORMAL’ was not declared in this scope
error: ‘ShellExecute’ was not declared in this scope

i have includede th windows.h but why these two error in linux.

Is there any other way to open the url in browser using linux platform.
Please reply me. i wait urs reply.

Any other library.is required here??

please give some idea.

Thanks.
Nov 6 '08 #10

10K+
P: 13,264
windows.h is a Windows specific header file.
Surely you could have inferred that from the name.
Nov 6 '08 #11

P: 23
windows.h is a Windows specific header file.
Surely you could have inferred that from the name.
Ok, i know this. it is working in windows. can u give me some idea how to open a browser in linux using shellexcute() of windows.h.

thanks.
Nov 6 '08 #12

Expert 10K+
P: 11,448
Priceless!

kind regards,

Jos
Nov 6 '08 #13

10K+
P: 13,264
mamul, please do not start multiple threads for the same topic. I have already deleted two other threads of yours on this same topic.
Nov 6 '08 #14

P: 23
mamul, please do not start multiple threads for the same topic. I have already deleted two other threads of yours on this same topic.
Hi, Its very urgent for me, i am trying but unableto solve this. thas teh reason to post many times, if some one knows then please reply me.
Nov 6 '08 #15

Expert 10K+
P: 11,448
Hi, Its very urgent for me, i am trying but unableto solve this. thas teh reason to post many times, if some one knows then please reply me.
That is not a reason for us; if you happen to be in a hurry it is no reason to flood
the forum(s) with your question over and over again. It isn't a reason to stress up
the experts here. You should've asked your question earlier; that is just a bit of
time management. Now you just exhibit yourself as a little PITA. This is not a
free homework service; you have to do it yourself and we can possibly help you
when you're stuck. If you are unable to solve you assignment you'll fail classes
and most likely programming is not your cup of tea. Think about it.

Jos (moderator)
Nov 6 '08 #16

100+
P: 424
this is working for windows, but i want make work in linux.
Of course ShellExecute won't work because it's a windows command, as you might have guessed from the header windows.h. There is no portable way of opening something in a web browser so the solution will be OS-specific.
system("http://www.google.co.in/");
' http: ' is not recognized as an internal and external command.
so please tell me wt is the problem.
Try type http://www.google.co.in/ on the command line and you'll get the same error. This is because http://www.google.co.in/ is obviously a url, not a system command. Figure out how to open a url in your browser from the command line first then put that command in a system() call. See reference for system()
Nov 6 '08 #17

Banfa
Expert Mod 5K+
P: 8,916
Ok, i know this. it is working in windows. can u give me some idea how to open a browser in linux using shellexcute() of windows.h.
If you really wanted to do this then you would have to write a version of ShellExecute to run on Linux.

The real solution is not to use ShellExecute but to abstract the function at a higher level.

That is you write a function call OpenWebPage which could have a prototype of

bool OpenWebPage(const string filepath);

You then provide separate implementations for Windows and Linux of this function using #defined symbols to conditionally compile the correct code for the system.

The Windows version could call ShellExecute, the Linux version would have to call something else.
Nov 6 '08 #18

Banfa
Expert Mod 5K+
P: 8,916
mamul, I have merged your 2 remaining threads on this 1 subject.
Nov 6 '08 #19

Post your reply

Sign in to post your reply or Sign up for a free account.