#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<ctime>
using namespace std;
int main()
{
//写入指定路径
string fileName1 = "", fileName2 = "";
fileName1 += "E:\\";
string tmp = to_string(1);
fileName1 += tmp;
fileName1 += ".txt";
ofstream out(fileName1.c_str());//将fileName转化为c型字符串作为文件名
out << "how are u";
//读取大文件
clock_t start = clock();
string objpath = "1.txt";
ifstream fin(objpath, std::ios::binary);
//seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。
//指针定位在文件结束处,.tellg()取范围的大小,这里是整个文件的大小
vector<char> buf(fin.seekg(0, std::ios::end).tellg()); //ios::end:表示输入流的结束位置
//ios::beg:表示输入流的开始位置
fin.seekg(0, std::ios::beg).read(&buf[0], static_cast<std::streamsize>(buf.size()));
fin.close();
clock_t end = clock();
cout << "time : " << ((double)end - start) / CLOCKS_PER_SEC << "s\n";
return 0;
}
C++文件操作
最新推荐文章于 2023-05-27 20:05:51 发布