【发布时间】:2020-02-27 18:25:48
【问题描述】:
我正在尝试确定我正在读取的文件有多大(以字节为单位),因此我使用 Fseek 跳转到末尾并触发了错误:file.exe 已触发断点。 赫斯代码: 文件实用程序.cpp: #include "FileUtils.h"
namespace impact {
std::string read_file(const char* filepath)
{
FILE* file = fopen(filepath, "rt");
fseek(file, 0, SEEK_END);
unsigned long length = ftell(file);
char* data = new char[length + 1];
memset(data, 0, length + 1);
fseek(file, 0 ,SEEK_SET);
fread(data, 1, length, file);
fclose(file);
std::string result(data);
delete[] data;
return result;
}
}
FileUtils.h:
#pragma once
#include <stdio.h>
#include <string>
#include <fstream>
namespace impact {
std::string read_file(const char* filepath);
}
如果需要更多信息,请向我咨询,我非常乐意提供更多信息!
【问题讨论】:
-
FILE* file = fopen(filepath, "rt");-- 你没有检查文件是否打开成功。 -
更多关于如何使用fstream判断文件大小,建议参考链接:stackoverflow.com/questions/2409504/…stackoverflow.com/questions/5840148/…
标签: c++ visual-studio fstream