fstream f1;
f1.open(path,ios_base::binary|ios_base::in);
f1.read(buffer, sizeof(f1));
但是遇到字节00以后直接结束,后面的字节就不读取了.请问如何解决.
sizeof(f1),这个是fstream的长度不是数据的长度
应该有获取长度方法getleng
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
fstream fs("fstream.txt", ios::in | ios::out | ios::trunc);
if (!fs.bad())
{
// Write to the file.
fs << "Writing to a basic_fstream object..." << endl;
fs.close();
// Dump the contents of the file to cout.
fs.open("fstream.txt", ios::in);
cout << fs.rdbuf();
fs.close();
}
}
const char* pFilePath = "D:\\1.txt";
FILE *pFile = NULL;
fopen_s(&pFile, pFilePath, "rb");
fseek(pFile, 0, SEEK_END); //定位到文件末尾
int len = ftell(pFile); //求文件长度
char *pBuf = new char[len];
rewind(pFile); //重新定位指针到文件开始处
fread(pBuf, 1, len, pFile);
fclose(pFile);