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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mamul
    New Member
    • Oct 2008
    • 23

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

    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.ope n( 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.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I would use system( "whatbrowseryou like whatyourfileisc alled"); 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.
    Last edited by mac11; Nov 5 '08, 01:57 PM. Reason: typo

    Comment

    • arnaudk
      Contributor
      • Sep 2007
      • 425

      #3
      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_MACH INE\SOFTWARE\Cl asses\.htm]
      @="FirefoxHTM L"

      Then go to the FirefoxHTML key:

      [HKEY_LOCAL_MACH INE\SOFTWARE\Cl asses\FirefoxHT ML\shell\open\c ommand]
      @="C:\\PROGRA~1 \\MOZILL~1\\FIR EFOX.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.

      Comment

      • george666
        New Member
        • Jul 2008
        • 28

        #4
        Originally posted by arnaudk
        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)

        Comment

        • lini
          New Member
          • Mar 2007
          • 12

          #5
          Take a look at this


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

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

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

          ch33rs,
          lini

          Comment

          • arnaudk
            Contributor
            • Sep 2007
            • 425

            #6
            Originally posted by george666
            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.

            Comment

            • arnaudk
              Contributor
              • Sep 2007
              • 425

              #7
              Originally posted by lini
              ShellExecute(NU LL, "open", "http://dreamincode.net ", NULL, NULL,SW_SHOWNOR MAL);
              Yes, that works for a URL, but try it with a file:

              ShellExecute(NU LL, "open", "file:///C:/Users/test.txt", NULL,NULL,SW_SH OWNORMAL);

              will open the file in the default viewer, not the web browser.

              Comment

              • mamul
                New Member
                • Oct 2008
                • 23

                #8
                Originally posted by lini
                Take a look at this


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

                void main()
                {
                ShellExecute(NU LL, "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

                Comment

                • mamul
                  New Member
                  • Oct 2008
                  • 23

                  #9
                  Originally posted by mac11
                  I would use system( "whatbrowseryou like whatyourfileisc alled"); 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.i n/");

                  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.

                  Comment

                  • mamul
                    New Member
                    • Oct 2008
                    • 23

                    #10
                    windows.h is not working in linux

                    Hi All,

                    #include"window s.h'
                    ShellExecute(NU LL, "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.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      windows.h is a Windows specific header file.
                      Surely you could have inferred that from the name.

                      Comment

                      • mamul
                        New Member
                        • Oct 2008
                        • 23

                        #12
                        Originally posted by r035198x
                        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.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Priceless!

                          kind regards,

                          Jos

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            mamul, please do not start multiple threads for the same topic. I have already deleted two other threads of yours on this same topic.

                            Comment

                            • mamul
                              New Member
                              • Oct 2008
                              • 23

                              #15
                              Originally posted by r035198x
                              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.

                              Comment

                              Working...