#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    int n=0;
    int sum=0;
    ifstream fin;
    fin.open("input.txt");
    for (int i=0; i<4; i++)
    {
        fin >> n;
        sum=sum+n;
    }
    cout << "Sum of given numbers is " << sum << endl;

    return 0;
}

in the above code, i want to fetch data from a url instead of local "input.txt" file, can i do that in c++?

libcurl is a commonly used library to do that sort of thing.

try to search c++ programming in apache (cgi) you will get a sample code.

Invadev

It is not "easy" code to write.
Remember there is a network and another machine involved, so many more kinds of error can occur.

What do you want from the web site pointed to by the URL? The HTML document, objects contained in an element like src of the <img> element?

If you are going to do that in windows, you can run an instance of the internet explorer thru its dispatch interface, feed that url to the interface and create a handler on the documentready event and parse the contents of the document thru document dispatch interface when the event fires up.

Small example.. Uhh you'll have to replace some of the functions with your own but you get the idea.

std::uint32_t RecvEx(SOCKET Socket, void* Buffer, std::uint32_t BufferLength)
{
    char* Pointer = reinterpret_cast<char*>(Buffer);
    std::uint32_t TotalRead = 0;

    while (BufferLength > 0)
    {
        int BytesRead = recv(Socket, Pointer, std::min(1024 * 1024, static_cast<int>(BufferLength)), 0);
        if (BytesRead < 0)
        {
            if ((BytesRead == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
                continue;

            throw std::runtime_error("Error! RecvEx: Failed To Read Bytes.");
        }

        if (BytesRead == 0) break;

        Pointer += BytesRead;
        BufferLength -= BytesRead;
        TotalRead += BytesRead;
    }

    return TotalRead;
}

std::string RecvLine(SOCKET Socket)
{
    char Character = 0;
    std::string Line;

    while(true)
    {
        RecvEx(Socket, &Character, 1);

        if (Character == '\r')
        {
            RecvEx(Socket, &Character, 1);
            if (Character == '\n') break;
            Line += '\r';
        }
        else if (Character == '\n') break;

        Line += Character;
    }
    return Line;
}

void RecvHeaders(SOCKET Socket, std::vector<std::string>* Header)
{
    while(true)
    {
        std::string Line = RecvLine(Socket);
        if (Line.size() == 0) return;
        if (Header) Header->push_back(Line);
    }
}

std::string FindHeader(const std::string &LineToFind, const std::vector<std::string> &Header)
{
    for (std::size_t I = 0; I < Header.size(); ++I)
    {
        int Position = Pos(":", Header[I]);
        if ((Position != -1) && !Header[I].compare(0, Position, LineToFind))
        {
            Position = PosFirstNotOf(" ", Header[I], Position + 1);
            if (Position != -1)
                return Header[I].substr(Position);
        }
    }
    return std::string();
}

std::uint32_t RecvChunkSize(SOCKET Socket)
{
    std::string Line = RecvLine(Socket);
    int Position = Pos(";", Line);
    if (Position != -1) Line.erase(Position);

    char* NextCharacter;
    std::uint32_t Value = strtoul(Line.c_str(), &NextCharacter, 16);

    return (*NextCharacter == '\0') ? Value : -1;
}


void GetPage(std::string WebPage, std::string SavePath)
{
    WSADATA wsaData;
    string Address;
    struct addrinfo *result;
    struct sockaddr_in  *sockaddr_ipv4;

    string Request = "GET / HTTP/1.1\r\n";
    Request += "Host: " + WebPage + "\r\n";
    Request += "Connection: close\r\n";
    Request += "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11\r\n";
    Request += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
    Request += "Accept-Language: en-US,en;q=0.8\r\n";
    Request += "Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7\r\n";
    Request += "Accept-Encoding: \r\n";
    Request += "Cache-Control: no-cache\r\n";
    Request += "\r\n";

    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) return;

    SOCKET Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    getaddrinfo(WebPage.c_str(), NULL, NULL, &result);
    if (result->ai_family == AF_INET)
    {
        sockaddr_ipv4 = (struct sockaddr_in *) result->ai_addr;
        Address = inet_ntoa(sockaddr_ipv4->sin_addr);
    }
    freeaddrinfo(result);


    SOCKADDR_IN SockAddr;
    memset(&SockAddr, 0, sizeof(SockAddr));
    SockAddr.sin_port = htons(80);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = inet_addr(Address.c_str());

    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) == SOCKET_ERROR) return;

    if (send(Socket, Request.c_str(), Request.size(), 0) == SOCKET_ERROR) return;
    shutdown(Socket, SD_SEND);

    std::vector<std::string> Headers;
    std::vector<unsigned char> Data;
    RecvHeaders(Socket, &Headers);

    std::string Encoding = FindHeader("Transfer-Encoding", Headers);
    if (Encoding == "chunked")
    {
        std::uint32_t ChunkLength = RecvChunkSize(Socket);

        while (ChunkLength)
        {
            size_t Offset = Data.size();
            Data.resize(Offset + ChunkLength);
            RecvEx(Socket, &Data[Offset], ChunkLength);
            RecvLine(Socket);
            ChunkLength = RecvChunkSize(Socket);
        }
    }
    else
    {
        std::string HeaderValue = FindHeader("Content-Length", Headers);
        if (HeaderValue.size())
        {
            std::uint32_t ContentLength = ToNumber<std::uint32_t>(HeaderValue);

            if (ContentLength > 0)
            {
                Data.resize(ContentLength);
                RecvEx(Socket, &Data[0], ContentLength);
            }
        }
        else
        {
            std::uint8_t Buffer[1024] = {0};

            while(true)
            {
                std::uint32_t BytesRead = RecvEx(Socket, Buffer, sizeof(Buffer));
                if (BytesRead == 0) break;

                std::size_t Offset = Data.size();
                Data.resize(Offset + BytesRead);
                std::memcpy(&Data[Offset], Buffer, BytesRead);
            }
        }
    }

    closesocket(Socket);
    WSACleanup();

    std::string Response = Replace(std::string(Data.begin(), Data.end()), "\r", "", ReplaceAll);

    std::fstream hFile(SavePath.c_str(), std::ios::out);
    hFile<<Response;
    hFile.close();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.