我有以下程序将文件读入字符串缓冲区。
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
constexpr int BUFSIZE = 1024;
int main(int argc, char *argv[])
{
std::ifstream ifs(argv[1], std::ifstream::binary);
if(!ifs)
return 1;
string buffer(BUFSIZE, L'\0');
ifs.read(&buffer[0], BUFSIZE);
cerr << ifs.gcount() << endl;
return 0;
}
它会打印出预期的1024。
以下应该读入wstring缓冲区的程序无法正常工作。
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
constexpr int BUFSIZE = 1024;
int main(int argc, char *argv[])
{
std::wifstream ifs(argv[1], std::ifstream::binary);
if(!ifs)
return 1;
wstring buffer(BUFSIZE, L'\0');
ifs.read(&buffer[0], BUFSIZE);
cerr << ifs.gcount() << endl;
return 0;
}
Ir用相同的文件打印出0。
如您所见,唯一的区别是将流更改为wstream,将缓冲区更改为wstring。
我已经在OpenSUSE Tumbleweed下尝试了g ++ 8.2.1和clang ++ 6.0.1。
问题/我的错误在哪里?
答案 0 :(得分:2)
对于UTF-16,您应该使用tt <- "date aaa bbb
2005-01-03 50.97 11
2005-01-04 49.98 23
2005-01-05 50.81 45
2005-01-06 50.48 56
2005-01-07 50.31 86
2005-01-10 50.98 34"
d <- read.table(text=tt, header=T)
和std::basic_ifstream<char16_t>
。 std::u16string
和std::wifstream
不合适,因为std::wstring
的宽度是实现定义的。特别是在Linux中,它通常是32位宽。
与字符文字相同。您应该使用wchar_t
等代替u'\0'
。