You are here:

C++/Downloading a file online.


I would like to know if it is possible to read a file into a stream to a file that is online using strictly C++.

for example, I tried this, but I failed...


#include <string>
#include <fstream.h>     
#include <iostream.h>

using namespace std;

string GetCfg(string, string);

string GetCfg(string passed_str, string file_name){
char chCfg;
string str;
string check_str;

ifstream cfg(file_name.c_str());
     while (!cfg.eof()){
        chCfg = cfg.get();
        if (chCfg == '='){
         if (passed_str == check_str) {
         while (!cfg.eof()){
         chCfg = cfg.get();
         if (chCfg == '\n')
         check_str += chCfg;
        if (chCfg =='\n'){


return str;

int main(){

cout << GetCfg("dbf", "");

return 0;


and I would like to read a file in the format of *.cgi.

In short, no not like this.

The file name you give is a URL and the first part of this specifies the internet protocol to use to access the file - in this case the HTTP protocol. This is a network protocol and usually implies the use of network sockets to access the data.

The supplied standard C++ library file streams work only for files mounted into a local file system using a name that represents an operating system file or, if directories are supported by the OS, a file directory path name. For remote files this usually means mounting or mapping a shared network drive, partition or folder using protocols such as NFS (UN*X) and SMB (Microsoft).

To access network data streams you have to use networking facilities. There are I believe various C++ stream implementations that wrap network sockets (for example try searching for: C++ iostream socket using you favourite Internet search site), as well as C++ networking libraries such as ACE ( One site that looked potentially useful is .

The next hurdle you will encounter is that raw socket streams (C++ IOStream style or otherwise) will allow you to connect to a remote computer by name or IP address using a given port (e.g. port 80 for an http server). Having connected you will have to send and receive data according the protocol used by the server you are talking to - HTTP in this case. The internet protocols are defined by IETF RFCs (see HTTP is covered by RFCs 1945 (version 1.0) and 2068 (version 1.1). The protocol for transferring files is usually FTP so you might like to read up on this protocol as well. If you are lucky you can track down libraries you can use that encapsulate these protocol for you. For example if you are using Microsoft's MFC then you might be interested in their WinInet classes, based on the Win32 WinInet functions (see ).

This site: has loads of free C / C++ libraries - there is a page for sockets, TCP and internet libraries here: - so hopefully you can find something useful.

Hope this helps a bit.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.