我正在完成一項家庭作業任務,需要我在同一個命令中重定向輸入和輸出。當搜索「<」和「>」命令(即字符串)時,if語句始終返回true。這是因爲它在64位系統上找到了無符號整數的最大值。find()返回unsigned int的最大值
if (uCommand.find("<",0) && uCommand.find(">", 0))
聲明將始終爲真。當我運行uCommand.find( 「>」,0)在gdb的回報18446744073709551615.
int main(int argc, char *argv[]) {
// local variables
char **toks;
int retval;
int ii;
int redirect;
int search;
string fileName;
// initialize local variables
toks = NULL;
retval = 0;
ii = 0;
// main (infinite) loop
while (true)
{
// get arguments
toks = gettoks();
if (toks[0] != NULL)
{
for (ii = 0; toks[ii] != NULL; ii++) {
uCommand += " ";
uCommand += (string)toks[ii];
}
// Search for input, output, or dual redirection
if (uCommand.find("<", 0) && uCommand.find(">", 0))
{
redirect = dualRedirect(toks, uCommand);
addHist(uCommand);
}
else if (uCommand.find("<", 0)) {
redirect = inRedirect(toks, uCommand);
addHist(uCommand);
}
else if (uCommand.find(">", 0)) {
redirect = outRedirect(toks, uCommand);
addHist(uCommand);
}
// Look for hist or r and execute the relevant functions
if (!strcmp(toks[0], "r"))
repeatCommand(uCommand);
else if (!strcmp(toks[0], "hist")) {
addHist(uCommand);
showHist();
}
else if (redirect == 0) {
execCommand(toks);
addHist(uCommand);
}
// Increment the command count. This only runs if a something is entered
// on the command line. Blank lines do not increment this value.
commandCount++;
}
}
// return to calling environment
return(retval);
}
最大的無符號整型是-1,如果interpretted作爲一個符號整型。你確定它不返回一個有符號的int嗎? –