c++ - 如何将 getline 与标准输入一起使用?
问题描述
std::stringstream line;
getline(stdin, line);
有人可以向我解释为什么我在尝试编译时收到“没有匹配的函数调用 getline”错误吗?
解决方案
C++ 没有getline
将FILE*
( stdin
) 和iostream
( line
) 作为参数。
相反,你应该
std::string line;
if (std::getline(std::cin, line)) // ensure read succeeded
{
std::stringstream linestream(line);
// opperate on linestream
}
与.std::cin
关联的 C++ IO 流在哪里?stdin
如果您绝对必须stdin
在原始版本中使用,在符合 POSIX 的系统上,您可以
char * line;
size_t len;
if (getline(&line, &len, stdin) != -1) // ensure read succeeded
{
std::stringstream linestream(line);
free(line); // release the buffer allocated by getline
// must free(line). Do not delete line;
// opperate on linestream
}
您已将其标记为 C++,因此我推荐前者。
推荐阅读
- java - 大量 org.springframework.beans.factory.support.DefaultListableBeanFactory 对象驻留在堆转储中
- php - 关于如何自动登录网站并将数据上传到mysql数据库的建议?
- optimization - 使用 pytorch 进行边界优化
- javascript - 创建上传图片的 URL 的 Json 文件
- python-3.x - 修改 QWeb 报告中的货币字符串(值)
- azure-synapse - Azure Synapse Notebook 从 Onprem 中提取数据?
- c++ - 为什么我们不能在 C++ 中转发声明具有已知大小的类/结构?
- python - 我打开了许多 csv 文件,现在我需要将这些文件的名称添加到
- server - 处理因批处理请求错误而导致 ID 为空的 JSON-RPC 响应的最佳实践是什么?
- sonarqube - 有人知道为什么会这样吗??我运行了“mvn compile”,然后从 sonarqube 运行命令