string类、字符串数组相关问题

来源:互联网 发布:前端程序员简历模板 编辑:程序博客网 时间:2024/05/24 07:28

1、如何将一个文本文件拷贝到一个字符串中?

ifstream inputFile("interestingData.txt");string fileData((istream_iterator<char>(inputFile)),istream_iterator<char>());
这虽然是一个可行的方法,但是不建议使用,因为这样会把文件中所有的空格忽略
建议使用:
ifstream inputFile("interestingData.txt");string fileData((istreambuf_iterator<char>(inputFile)),istreambuf_iterator<char>());
相关函数:
string cook;
1、cook.c_str()
它返回当前字符串的首字符地址.c_str函数的返回值是constchar*的,不能直接赋值给char*.


0 0
原创粉丝点击