C++无法返回变量
嘿大家!尽管 errcheck 将具有非零值,但该代码位始终返回 0。如果我使用 return 1;它按预期工作。请帮忙?
int errcheck = system(docommand.c_str());
if (errcheck != 0)
{
cerr << "Could not retrieve tarball!" << " Errcheck status (debug): " << errcheck << endl;
return errcheck;
}
这是完整的代码:
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
umask(0);
mkdir("/tmp/.aget", 0755);
chdir("/tmp/.aget");
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
string s1("wget -q http://aur.archlinux.org/packages/");
string s2("/");
string s3(".tar.gz");
docommand += s1;
docommand += target;
docommand += s2;
docommand += target;
docommand += s3;
cout << "Downloading AUR tarball for '" << target << "'..." << endl;
int errcheck = system(docommand.c_str());
if (errcheck != 0)
{
cerr << "Could not retrieve tarball!" << " Errcheck status (debug): " << errcheck << endl;
return errcheck;
}
}
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
string s1("tar xf ");
string s2(".tar.gz");
docommand += s1;
docommand += target;
docommand += s2;
cout << "Extracting '" << target << ".tar.gz'..." << endl;
system(docommand.c_str());
}
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
chdir("/tmp/.aget");
chdir(target.c_str());
system("makepkg -csim --noconfirm > /dev/null");
}
rmdir("/tmp/.aget");
return 0;
}
Hey all! This bit of code always returns 0, even though errcheck will have a non-zero value. If I use return 1; it works as expected. Please help?
int errcheck = system(docommand.c_str());
if (errcheck != 0)
{
cerr << "Could not retrieve tarball!" << " Errcheck status (debug): " << errcheck << endl;
return errcheck;
}
Here is the full code:
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
umask(0);
mkdir("/tmp/.aget", 0755);
chdir("/tmp/.aget");
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
string s1("wget -q http://aur.archlinux.org/packages/");
string s2("/");
string s3(".tar.gz");
docommand += s1;
docommand += target;
docommand += s2;
docommand += target;
docommand += s3;
cout << "Downloading AUR tarball for '" << target << "'..." << endl;
int errcheck = system(docommand.c_str());
if (errcheck != 0)
{
cerr << "Could not retrieve tarball!" << " Errcheck status (debug): " << errcheck << endl;
return errcheck;
}
}
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
string s1("tar xf ");
string s2(".tar.gz");
docommand += s1;
docommand += target;
docommand += s2;
cout << "Extracting '" << target << ".tar.gz'..." << endl;
system(docommand.c_str());
}
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
chdir("/tmp/.aget");
chdir(target.c_str());
system("makepkg -csim --noconfirm > /dev/null");
}
rmdir("/tmp/.aget");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Unix 退出状态仅限于值 0-255,即无符号 8 位整数的范围。因此,您看不到 2048。
有关更多信息,请参阅退出状态 wiki 页面。
Unix exit statuses are restricted to values 0-255, the range of an unsigned 8-bit integer. As such, you cannot see 2048.
See Exit Status wiki page for more information.
我怀疑 wget 总是返回 0。
这是因为来自 http 请求的实际错误状态位于流中。
I suspect that wget always returns 0.
This is because the actual error status from an http request is in the stream.