C++中是否有一个像c中的getdelim函数一样的函数?我想用std :: ifstream对象来处理一个文件,所以我不能在这里使用getdelim。 任何帮助将不胜感激。 谢谢。有没有像getdelim是C++的函数?
0
A
回答
4
函数getline,既为的std :: string免费的功能和字符缓冲区的成员有过载采取了分隔符(BTW getdelim是GNU扩展)
1
如果你可以使用升压那么我建议Tokenizer库。下面的示例使用空格和分号分隔标记化流:
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
#include<algorithm>
int main() {
typedef boost::char_separator<char> Sep;
typedef boost::tokenizer<Sep> Tokenizer;
std::string str("This :is: \n a:: test");
Tokenizer tok(str, Sep(": \n\r\t"));
std::copy(tok.begin(), tok.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
输出:
This
is
a
test
如果你想标记输入的内容流也很容易做到:
int main() {
std::ifstream ifs("myfile.txt");
typedef std::istreambuf_iterator<char> StreamIter;
StreamIter file_iter(ifs);
typedef boost::char_separator<char> Sep;
typedef boost::tokenizer<Sep, StreamIter> Tokenizer;
Tokenizer tok(file_iter, StreamIter(), Sep(": \n\r\t"));
std::copy(tok.begin(), tok.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
相关问题
- 1. 在c中有没有像c()中的clock()函数?
- 2. 有没有像返回一个函数?
- 3. 没有C++ 0x的C++ lambda函数?
- 4. C#函数没有像预期的那样返回字符串
- 5. 没有构造函数的C#类
- 6. 没有匹配的函数调用C++
- 7. 没有头文件的C函数
- 8. 没有虚函数的C++接口
- 9. C++:没有重载函数的实例?
- 10. 没有构造函数的实例C++
- 11. C++ - 没有匹配的构造函数
- 12. C函数没有参数行为
- 13. 有没有像c + + const在c#中?
- 14. 有没有像C++的Python的StringIO/CStringIO?
- 15. 有没有办法在C#中挂接托管函数,就像我在C++中的非托管函数一样?
- 16. C有没有像PHP ob_start的东西?
- 17. 在C#中有没有像Python的getattr()?
- 18. 有没有像样的Websocket C#实现?
- 19. C有没有像IDLE(python)的东西?
- 20. 是否有可能创建一个没有参数的C varargs函数?
- 21. C++析构函数没有被调用?
- 22. C++函数没有返回值
- 23. 为什么C没有snwprintf函数?
- 24. C++ - Arduino - 没有匹配函数调用
- 25. C#没有运行派生类函数
- 26. Visual C#中没有析构函数吗?
- 27. 在VB.NET或C#中没有Form.Submit()函数?
- 28. C函数没有正确计算
- 29. 有没有像ASP.NET MVC的PHP函数nl2br()?
- 30. 有没有像MYSQL IN这样的PHP函数?
getdelim不完全是一个GNU扩展:我刚刚在http://www.opengroup.org/onlinepubs/9699919799/functions/getline.html – mkluwe 2010-02-12 11:05:32
上发现它是一个开放组规范,感谢info..ya我知道getdelim不是一个标准的C函数,但它只适用于FILE *。 – assassin 2010-02-12 11:11:16
还有一个问题......我如何使用getline函数检查文件结束? Coz,不推荐使用.eof(),因为它不会提示eof,直到我尝试读取超出eof。 – assassin 2010-02-12 11:16:27