Call me Alex
Call me Alex

Reputation: 11

How to open web urls using C language?

I know the function system("START www.weburl.com").
However, I need to change link during program running and open different parts of that website.
I have to add to that link, for example, www.weburl.com/i, where i is my integer.

I can not do it manually every time, so my question is how can I open webpages based on my strings from the program?

Upvotes: 1

Views: 4421

Answers (1)

You can make a new string that uses the number you want, then call system on that:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

void open_website_part(int i) {
    char *cmd;
    asprintf(&cmd, "START www.weburl.com/%d", i);
    system(cmd);
    free(cmd);
}

Or if your libc doesn't have asprintf available:

#include <stdio.h>
#include <stdlib.h>

#define FORMAT_STRING "START www.weburl.com/%d"
void open_website_part(int i) {
    size_t len = snprintf(NULL, 0, FORMAT_STRING, i) + 1;
    char *cmd = malloc(len*sizeof(char));
    snprintf(cmd, len, FORMAT_STRING, i);
    system(cmd);
    free(cmd);
}

Upvotes: 1

Related Questions