- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为自己开发一个受限版本的 shell。这是一个C++程序
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
using namespace std;
const int MAX = 256;
const int CMD_MAX = 10;
char *valid_cmds = " ls ps df ";
int main()
{
char line_input[MAX], the_cmd[CMD_MAX];
char *new_args[CMD_MAX], *cp;
int i;
while (1) {
cout << "cmd> ";
if (cin.getline(line_input, MAX, '\n') != NULL) {
cp = line_input;
i = 0;
if ((new_args[i] = strtok(cp, " ")) != NULL) {
strcpy(the_cmd, new_args[i]);
strcat(the_cmd, " ");
if ((strstr(valid_cmds, the_cmd) -valid_cmds)% 4 == 1) {
do {
cp = NULL;
new_args[++i] = strtok(cp, " ");
} while (i < CMD_MAX
&& new_args[i] != NULL);
new_args[i] = NULL;
switch (fork()) {
case 0:
execvp(new_args[0], new_args);
perror("exec failure");
exit(1);
case -1:
perror("fork failure");
exit(2);
default:
// In the parent we should be waiting for
// the child to finis h
;
}
} else
cout << "huh?" << endl;
}
}
}
}
我无法理解的是,当我编译这个程序时,我在行中遇到了错误
(cin.getline(line_input, MAX, '\n') != NULL)
错误是在我做了一个$ g++ p3.7.cpp
p3.7.cpp:10:20: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
char *valid_cmds = " ls ps df ";
^~~~~~~~~~~~
p3.7.cpp: In function ‘int main()’:
p3.7.cpp:18:42: error: no match for ‘operator!=’ (operand types are ‘std::basic_istream<char>’ and ‘long int’)
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
p3.7.cpp:18:42: note: candidate: operator!=(int, long int) <built-in>
p3.7.cpp:18:42: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘int’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iosfwd:40:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/postypes.h:221:5: note: candidate: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/postypes.h:221:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::fpos<_StateT>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:64:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_pair.h:456:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_pair.h:456:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::pair<_T1, _T2>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:311:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator!=(const reverse_iterator<_Iterator>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:311:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:349:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator!=(const reverse_iterator<_IteratorL>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:349:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1130:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
operator!=(const move_iterator<_IteratorL>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1130:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1136:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
operator!=(const move_iterator<_Iterator>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1136:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:158:5: note: candidate: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator!=(const allocator<_T1>&, const allocator<_T2>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:158:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::allocator<_CharT>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:164:5: note: candidate: template<class _Tp> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:164:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::allocator<_CharT>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6044:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6044:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6057:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const _CharT* __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6057:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: mismatched types ‘const _CharT*’ and ‘std::basic_istream<char>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6069:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6069:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:46:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:319:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_code&)
operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:319:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:323:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_condition&)
operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:323:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:327:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_code&)
operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:327:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_condition&’
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:331:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_condition&)
operator!=(const error_condition& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:331:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_condition&’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_facets.h:48:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_ios.h:37,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:44,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/streambuf_iterator.h:210:5: note: candidate: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/streambuf_iterator.h:210:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
我上面的错误是什么。我是否需要包含一些我忘记的头文件。
最佳答案
更多的是风格评论。我倾向于将您的主要功能分解为单独的功能,每个功能处理不同的问题。
在这个例子中,我使用了 C++ 数据结构,而不是 C 风格的数据结构。
我还使用了 boost::optional
和 boost::tokenizer
。
boost 库套件对任何 c++ 开发人员来说都是无价的,因为它提供了 c++ 标准库无法提供的许多基本应用程序编程工具。
#include <boost/optional.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
using boost::optional;
using std::string;
using std::vector;
void prompt(std::istream& is)
{
if (std::addressof(is) == std::addressof(std::cin))
{
std::cout << "cmd> ";
}
}
optional<string> next_line(std::istream& is)
{
auto result = optional<string>();
std::string line;
while (1)
{
prompt(is);
if (!getline(is, line)) break;
if (!line.empty())
{
result = line;
break;
}
}
return result;
}
vector<string> tokenise(std::string const& source)
{
using char_function = boost::char_separator<char>;
auto tokens = boost::tokenizer<char_function>(source, char_function(" "));
return vector<string>(tokens.begin(), tokens.end());
}
auto to_char_data_vector(vector<string>::iterator first, vector<string>::iterator last) -> vector<char *>
{
auto to_char_data = [](std::string& str) { return str.data(); };
auto result = vector<char *>();
std::transform(first, last,
back_inserter(result),
to_char_data);
return result;
};
auto append_null(vector<char*> v) -> vector<char *>
{
v.push_back(nullptr);
return v;
}
int sub_command(string const& cmd, vector<string>& args) // note - not const.
{
auto do_error = [](const char *message)
{
auto retcode = errno;
std::cerr << message << std::endl;
return retcode;
};
auto pid = fork();
if (pid == -1)
{
return do_error("fork failure");
}
else if (pid == 0)
{
auto result = execv(cmd.data(),
append_null(to_char_data_vector(begin(args),
std::end(args)))
.data());
std::exit(do_error("execv failure"));
}
else
{
int stat = 0;
auto waitresult = waitpid(pid, &stat, 0);
if (waitresult == -1)
return do_error("failed to wait");
return WEXITSTATUS(stat);
}
}
/// @pre tokens.length() >= 1
int interpret(vector<string> const& tokens)
{
auto args = vector<string>(next(begin(tokens)), std::end(tokens));
auto& cmd = tokens[0];
if (boost::iequals(cmd, "exit"))
{
std::exit(args.empty() ? 0 : std::stoi(args[0]));
}
else
{
return sub_command(cmd, args);
}
}
int main()
{
while (auto opt_line = next_line(std::cin))
{
auto tokens = tokenise(*opt_line);
if (tokens.empty()) continue;
auto return_code = interpret(tokens);
std::cout << "command returned: " << return_code << std::endl;
}
}
运行示例:
cmd> /bin/ls .
CMakeCache.txt TryThings.cbp trythings
CMakeFiles _3rdParty
Makefile cmake_install.cmake
command returned: 0
cmd> exit 20
Process finished with exit code 20
请注意,我必须指定 ls
命令的确切路径。 evecv
对搜索路径一无所知。
关于c++ - cin.getline() 开发 shell 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023389/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!