#include <fstream>
string getDataByStream(string filePath) {
ifstream infile;
infile.open(filePath.data());
assert(infile.is_open());
string buf;
string resStr;
while (getline(infile, buf))
{
resStr += buf;
}
return resStr;
}
string getDataByFile(string filePath) {
FILE* file = fopen(filePath.c_str(), "rb");
string resStr;
while (true) {
char buf = fgetc(file);
if (feof(file)!=1) {
resStr += buf;
}
else {
break;
}
}
return resStr;
}
string getDataByFileBuffer(string filePath) {
char buf[1024];
FILE* fp = fopen(filePath.c_str(), "rb");
stringstream sstr;
while (fgets(buf, 1024, fp) != NULL) {
sstr << buf;
}
sstr.clear();
fclose(fp);
return sstr.str();
}
C++几种读取文件的方式
于 2022-11-18 12:02:07 首次发布