使用libcurl库和调用curl使用system() [英] Using the libcurl library vs calling curl using system()

查看:557
本文介绍了使用libcurl库和调用curl使用system()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的程序从一个链接下载一些音频文件,并保存它。



我知道这可以很容易地在命令行使用 curl (例如: curl -AMozillawww.example.com> hello.mp3 p>

我看到使用 system()运行 curl (即它看起来像 system(curl -AMozillawww.example.com> hello.mp3)。即使这是一个容易的解决方案



最好的做法是使用匹配的库(在这种情况下为libcurl)编写一个等效的代码。



你们认为是什么?



PS - 这是一个普遍的问题,我的意思是有很多命令行程序可以由system()来运行,以获得一个快速而简单的结果。问题是如果可以使用这个方法来实现它。

解决方案

是的,最好直接使用 libcurl



这样,你可以避免:




  • 系统调用的费用

  • 产生新进程的费用

  • 潜在的安全相关错误在您的系统调用中



从shell中调用 curl 新shell和新进程没有理由,然后继续使用 libcurl 在该进程内。切出中间人。


I want my program to download some audio file from a link I give it and save it.

I know this can be easily done in the command line using curl (for instance: curl -A "Mozilla" "www.example.com" > hello.mp3

I saw examples where system() was used to run curl (i.e it looked something like system(curl -A "Mozilla" "www.example.com" > hello.mp3) . Even though this is an easy solution it seems bad to me.

Would it be better practice to write an equivalent code using the matching library (libcurl in this case)?

What do you guys think?

P.S - This is a general question in a sense. What I mean by that is that there are many command line programs which can be run by system() to get a fast and easy result. The question is if it's okay to use this method to achieve it.

解决方案

Yes, it would be better to use libcurl directly. That's what it exists for.

That way, you avoid:

  • the cost of a system call
  • the cost of spawning a new process
  • potential security-related bugs in your system call

Invoking curl from the shell will basically just spawn a new shell and new process for no reason, then go ahead and use libcurl inside that process anyway. Cut out the middle man.

这篇关于使用libcurl库和调用curl使用system()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆