C++流错误未知
C++ fstream error unknown
我正在尝试为一个文件制作一个包装器,所以这是一个fstream的小包装器。我正在制作一些想要读/写二进制和文本到文件的东西,所以我可以让模型加载程序以同样的方式说话。
我有一个问题:当我在ObjLoader.cpp中用这个调用时,为什么我的文件不打开?
Scatterbrain::Log *_file = new Scatterbrain::Log( path, false, true );
if( ! _file->Works() )
std::cout << "Error!!";
在散漫的大脑里有这个。h?我确信我已经包含了必要的头,因为一切都编译得很好,所以我认为这一定是我编写文件打开调用的方式的语义问题?-它正在被调用。。
namespace Scatterbrain
{
class Log
{
private:
std::string name;
bool rOnly;
bool isBinary;
int numBytes;
std::fstream file;
protected:
virtual int SizeBytes() { numBytes = (file) ? (int) file->tellg() : 0; return numBytes; }
public:
Log(){}
Log( std::string filename, bool append, bool readOnly )
{
if(FileExists(filename))
{
name = filename;
rOnly = readOnly;
file.open( name.c_str(), ((readOnly) ? int(std::ios::out) : int(std::ios::in |std::ios::out)) | ((append) ? int(std::ios::app) : int(std::ios::trunc)) );
}
}
virtual bool Works() { return (file.is_open() && file.good() ); }
感谢
关于这一切,有很多话可以说,所以我只把它放在评论中:
class Log
{
private:
std::string name;
bool rOnly;
std::fstream file;
public:
Log(){}
Log( std::string filename, bool append, bool readOnly)
: name(filename), // Use initializer lists
rOnly(readOnly),
file(filename, (readOnly ? std::ios::out : std::ios::in | std::ios::out) |
(append ? std::ios::app : std::ios::trunc))
{
// Why check if the file exists? Just try to open it...
// Unless, of course, you want to prevent people from creating
// new log files.
}
virtual bool Works()
{
// Just use the fstream's operator bool() to check if it's good
return file;
}
};
简而言之:
- 使用成员初始值设定项列表
- 不使用
new
。。。我不知道你为什么会在一开始,也不知道如果是这样,为什么会编译它 - 使用
operator bool()
函数查看它是否"良好"
相关文章:
- 双链表的擦除值函数,未知错误
- 未知C++错误:致命错误:glibc检测到无效的stdio句柄
- 带有unique_ptr的通用单链表,MS visual studio C++中存在未知错误
- 在最坏的情况下试验快速排序.它运行良好,但在最坏的情况下发生未知错误.我想
- C++ [] 重载未知错误
- 打电话时返回未知错误
- C 包括文件会导致未知错误
- Qtcpserver 仅在调试器下在侦听函数上返回未知错误
- std::p romise<void> 抛出未知错误,除非调用睡眠
- 如何在将我的 QT 应用程序交叉编译为 raspberry-pi3 时修复未知错误
- 使用reinterpret_cast的未知错误
- 如何修复QFile打开错误(未知错误),即使文件存在
- 使用c++特征库时出现未知错误
- 使用特征库的 mingw 出现未知错误
- CUDA 未知错误后 cuBLAS 调用
- 未知错误,调试器只给我一个内存地址
- 在子类之外实现函数导致未知错误
- 影响格雷厄姆查找凸包算法的未知错误
- 未知错误(-1000)执行cl_command_ndrange_kernel
- QT NetworkAccessManager未知错误