据我了解,cin.getLine 获取第一个字符(我认为它是一个指针)然后获取长度。当 cin 用于 char 时,我使用过它。我有一个函数返回指向数组中第一个字符的指针。是否有等效项可以将数组的其余部分转换为我可以使用整个数组的字符。我在下面解释了我要做什么。该函数运行良好,但如果有帮助,我可以发布该函数。
cmd_str[0]=infile();// get the pointer from a function
cout<<"pp1>";
cout<< "test1"<<endl;
// cin.getline(cmd_str,500);something like this with the array from the function
cout<<cmd_str<<endl; this would print out the entire array
cout<<"test2"<<endl;
length=0;
length= shell(cmd_str);// so I could pass it to this function
最佳答案
您可以使用字符串流:
char const * p = get_data(); // assume null-terminated
std::istringstream iss(std::string(p));
for (std::string line; std::getline(iss, line); )
{
// process "line"
}
如果字符数组不是以 null 结尾但具有给定大小 N
,则改为说 std::string(p, N)
。
关于c++ - cin.getline() 在从函数中获取 char 时等效。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158198/