c++ - cin.getline() 在从函数中获取 char 时等效。

标签 c++ arrays char

据我了解,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/

相关文章:

C++ - 函数范围内的函数声明?

javascript - 检索键等于值的javascript数组对象数据

c - 字符串正在打印奇怪的字符 - lex 中的 c 代码

c - 拆分从套接字接收的字符数组的最佳方法

c++ - msvcr100 相对于 msvcrt 的优势

C++11 简化了调用同一模板函数的不同特化的模板函数的语法

java - 理解两个排序数组的中位数算法

c++ - 展开多维数组指针失败c++

java - 检查字母是否大写的更快方法(性能)?

C++ cout打印缓慢