【发布时间】:2011-08-14 02:10:39
【问题描述】:
大家好!这段代码总是返回 0,即使 errcheck 有一个非零值。如果我使用 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;
}
【问题讨论】:
-
显示周边代码,这段代码所在的函数或方法的返回类型是什么?
-
显示函数原型以及如何检查返回值。可能是符号问题。返回值限制等
-
好的,我添加了完整的源代码。
-
你确定它是从那里退出,而不是在最后的
return 0;? -
1) 你怎么知道调用的是
return errcheck;,而不是return 0? 2)你怎么知道main返回0?