473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
18 3170
mac11
256 100+
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
arnaudk
424 256MB
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
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
lini
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
arnaudk
424 256MB
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
arnaudk
424 256MB
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
mamul
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
mamul
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
mamul
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
r035198x
13,262 8TB
windows.h is a Windows specific header file.
Surely you could have inferred that from the name.
Nov 6 '08 #11
mamul
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
JosAH
11,448 Expert 8TB
Priceless!

kind regards,

Jos
Nov 6 '08 #13
r035198x
13,262 8TB
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
mamul
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
JosAH
11,448 Expert 8TB
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
arnaudk
424 256MB
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
mamul, I have merged your 2 remaining threads on this 1 subject.
Nov 6 '08 #19

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: Dalan | last post by:
Perhaps someone can share information on the methods to use to effect the automation process of creating the property to set the AllowBypassKey function. I was directed to:...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
3
by: tigrrgrr42 | last post by:
I am working(vb.net03and05) with word documents stored in a sql db and I am currently bringing them from a byte array into a temp file to pop into word and make word do its thing as a com object. ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.