Abhishek
Abhishek

Reputation: 1837

How to fetch data from remote url using c++ program in linux?

I wish to read an XML file hosted on web via my C++ program. I'm working on LINUX. I'm currently using popen to read.

FILE* remote = popen("curl 'my_url_to_xml', "r")
fread(buf, 1024, 1, remote);

It works and gives me the content in the url but then it has this extra matter dumped on screen even though I'm not printing anything. Also the entire operation takes ~3000 ms. This is too much for my use case.

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
0    71    0    71    0     0    309      0 --:--:-- --:--:-- --:--:--     0

Any way to fix this or an alternative to popen? I wish to avoid third party libraries like libcurl. Any native solution ?

EDIT -- OK I accept even non native solutions are accepted. My first concern is popen. If this dumping problem can be fixed, I'll go with popen itself.

Upvotes: 1

Views: 1079

Answers (2)

David
David

Reputation: 6571

Call curl with the silent switch: -s OR --silent

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249153

I wish to avoid third party libraries like libcurl. Any native solution ?

No. C++ is not a "web language" and it has zero support for XML or HTTP built in. You need to use a library like libcurl. Or another programming language. Or reimplement one of those yourself.

Seriously, just use libcurl or similar and be done with it. Or even better, use Python. :)

Upvotes: 1

Related Questions