Thread: Open a WebPage

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    13

    Open a WebPage

    Is there a way to open a web page in C?

    I'm a newbie in this, im just starting... My goal is to open a link to a web page after the user enters the correct password. I can post my code so far if you ask for it, where it asks for the password and when it gets the correct one it opens up a program (iexplore.exe).

    please help

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yuo can use system() call to run the external program in C
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    ok so i didn't know how to attach or write my code here.. but I read the rules!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char* pass;
        char* p="hello";
        int i=0;
    
        printf("Enter password and press ENTER.\n \n");
        do
        {
            printf("Password: ");
            scanf( "%s",  pass);
    
            if(strcmp ( pass, p ) == 0)
            {
                printf("Correct Password. \n \n");
                i=10;
            }
            else
            {
                if(strcmp ( pass, "hint" ) == 0)
                {
                    printf("Password Hint: Form of salutation. \n \n");
                }
                else
                {
                    printf("Incorrect Password. \nType 'hint' for help. \n \n");
                    i++;
                }
            }
    
            if (i == 3)
            {
                i++;
                printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
                getch();
            }
        }
        while (i<4);
    
        if (i == 10)
        {
            printf("Press ENTER to continue... \n");
            getch();
            //Open a program or file
            system ("C:\\PROGRA~1\\INTERN~1\\iexplore\.exe");
            printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
            getch();
        }
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    Quote Originally Posted by vart View Post
    yuo can use system() call to run the external program in C
    so as you can see now, i did use the system() to call for iexplore.exe, but how do i open a specific page? i dont want it to open the homepage. i've been reading also some kind of changing the registry keys of windows so the c program would change the homepage, but that's kind of intrusive!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In all likelihood, if you supply a URL as a command line argument to iexplore, it will open that URL. This means that you need to concatenate the desired URL to the string that you pass to system().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    Quote Originally Posted by laserlight View Post
    In all likelihood, if you supply a URL as a command line argument to iexplore, it will open that URL. This means that you need to concatenate the desired URL to the string that you pass to system().
    i don't get it... :S
    like this?

    this is only the end....
    Code:
            //Open a program or file
            open (webpage);
            printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
            getch();
        }
    
        return 0;
    }
    
    void open(char *webpage)
    {
        system ("C:\\PROGRA~1\\INTERN~1\\iexplore.exe");

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    As laserlight pointed out concatenate the URL string to the iexplorer executable.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    Quote Originally Posted by itCbitC View Post
    As laserlight pointed out concatenate the URL string to the iexplorer executable.
    im sorry but i've been trying and i dont get the concatenate...
    i searched and i think i got the the concatenate right.. but is not working like that, it just ends. this is what i got:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char* internet="C:\\PROGRA~1\\INTERN~1\\iexplore.exe";
    char* url="www.google.com";
    
    
    
    int main()
    {
        char* pass;
        char* p="hello";
        int i=0;
    
        printf("Enter password and press ENTER.\n \n");
        do
        {
            printf("Password: ");
            scanf( "%s",  pass);
    
            if(strcmp ( pass, p ) == 0)
            {
                printf("Correct Password. \n \n");
                i=10;
            }
            else
            {
                if(strcmp ( pass, "hint" ) == 0)
                {
                    printf("Password Hint: Form of salutation. \n \n");
                }
                else
                {
                    printf("Incorrect Password. \nType 'hint' for help. \n \n");
                    i++;
                }
            }
    
            if (i == 3)
            {
                i++;
                printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
                getch();
            }
        }
        while (i<4);
    
        if (i == 10)
        {
            strcat(internet, url);
            printf("Press ENTER to continue... \n");
            getch();
            //Open a program or file
            system (internet);
            printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
            getch();
        }
    
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    supply in args when making the system call.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your code doesn't work. Because:
    1) http://apps.sourceforge.net/mediawik..._be_const_char
    2) http://apps.sourceforge.net/mediawik...tle=Scanf_woes
    3) I do not recommend you use Internet Explorer. When people see IE launching instead of their default browser, they get annoyed. I suppose you use Windows? Then you should look into ShellExecute. I believe a quick search on the forums should provide a good deal of information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Space(s) separate the command from its argument and strcat() won't do that for you.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    ok i got it with the shellexecute! thanks to everyone!!
    though i still got some memory error at the end :S.
    will anyone care to suggest something more?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main()
    {
        char* pass;
        char* p="hello";
        int i=0;
    
        printf("Enter password and press ENTER.\n \n");
        do
        {
            printf("Password: ");
            scanf( "%s",  pass);
    
            if(strcmp ( pass, p ) == 0)
            {
                printf("Correct Password. \n \n");
                i=10;
            }
            else
            {
                if(strcmp ( pass, "hint" ) == 0)
                {
                    printf("Password Hint: Form of salutation. \n \n");
                }
                else
                {
                    printf("Incorrect Password. \nType 'hint' for help. \n \n");
                    i++;
                }
            }
    
        }
        while (i<3);
    
        if (i == 10)
        {
            printf("Opening program... \n\n");
                    //Open a program or file
            ShellExecute(NULL, "open", "http://www.microsoft.com", NULL, NULL, SW_SHOWNORMAL);
            printf("Close program to contione. \nPress any key. \n\n");
            getch();
        }
    
        printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
        getch();
    
        return 0;
    }

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf( "%s", pass);

    your pass is not initialized pointer - you need a real buffer to store the user input

    Code:
    char pass[100];
    if(scanf("%99s", pass) == 1)
    {
       /* input succeded - process the pass buffer */
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, string literals should be const char*:
    const char* p="hello";
    Glad to see you took my advice about ShellExecute, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    Cool uhh duhh?

    this works..

    system ("START www.website_name_here.com");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 08:53 PM
  2. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 1
    Last Post: 11-23-2001, 10:01 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM