Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;调试断言错误 < >我一直在玩C++,决定写一个程序,它涉及到以二进制模式打开和写入文件。我不太熟悉C++的IoSoad功能(我主要是做基于API的编程),但我阅读了几个关于这个主题的技术指导,并编写了一些代码。该代码用于打开一个文件,将其数据读取到缓冲区,然后将该缓冲区转换为另一种格式并写入另一个文件。问题是它不断抛出“调试断言”错误,这显然是围绕无效使用空指针而产生的。然而,当我浏览代码时,我无法理解它。我可能只是误用了iostream库或者犯了一个简单的逻辑错误。我需要有单独的SetMemBlock函数,因为我计划使用相同的基来格式化各种函数上的不同输出。这只是我的原型。无论如何,这里是我的快速n'dirty类设置: const DebugMode = true; class A { public: bool FileFunction( const char *, const char * ); protected: bool SetMemBlock( char *, std::fstream &, std::streamoff & ); private: std::fstream SrcFileStream; std::fstream DestFileStream; }; bool A::SetMemBlock( char* MemBlock, std::fstream & FileStream, std::streamoff & Size ) { std::streamoff TempOff = 0; //This is meant to check for a non-empty buffer and to see if the stream is valid. if( MemBlock != 0 || !FileStream.is_open() ) return false; TempOff = FileStream.tellg(); FileStream.seekg(0, std::ios::end); Size = FileStream.tellg(); MemBlock = new( std::nothrow ) char[ (int) Size ]; if( MemBlock == 0 ) return false; FileStream.seekg(0, std::ios::beg); FileStream.read( MemBlock, (int) Size ); if( !FileStream ) return false; FileStream.seekg(TempOff); return true; } bool A::FileFunction( const char * SrcFile, const char * DestFile ) { char * MemBlock = 0; std::streamoff Size = 0; SrcFileStream.open( SrcFile, std::ios::binary | std::ios::in ); DestFileStream.open( DestFile, std::ios::binary | std::ios::out ); if( !SrcFileStream.is_open() || !DestFileStream.is_open() ) return false; if( DebugMode ) { std::cout<<"Files opened succesfully...\nNow writing memory block..."<<std::endl; } if( !SetMemBlock( MemBlock, SrcFileStream, Size ) ) { std::cout<<"An error occured when reading to memory block!"<<std::endl; return false; } if( DebugMode ) { std::cout<<"Memory block written..."<<std::endl; } DestFileStream.seekp( std::ios::beg ); DestFileStream.write( MemBlock, Size ); SrcFileStream.close(); DestFileStream.close(); delete[] MemBlock; return true; } const DebugMode=true; 甲级 { 公众: 布尔文件函数(常量字符*,常量字符*); 受保护的: bool SetMemBlock(char*,std::fstream&,std::streamoff&); 私人: std::fstream SrcFileStream; std::fstream DestFileStream; }; bool A::SetMemBlock(char*MemBlock,std::fstream和FileStream,std::streamoff和Size) { 标准::streamoff TempOff=0; //这意味着检查非空缓冲区,并查看流是否有效。 if(MemBlock!=0 | |!FileStream.is_open()) 返回false; TempOff=FileStream.tellg(); seekg(0,std::ios::end); Size=FileStream.tellg(); MemBlock=new(std::nothrow)char[(int)Size]; if(MemBlock==0) 返回false; seekg(0,std::ios::beg); read(MemBlock,(int)Size); 如果(!FileStream) 返回false; FileStream.seekg(TempOff); 返回true; } bool A::FileFunction(const char*SrcFile,const char*DestFile) { char*MemBlock=0; 标准::流量大小=0; SrcFileStream.open(SrcFile,std::ios::binary | std::ios::in); DestFileStream.open(DestFile,std::ios::binary | std::ios::out); 如果(!SrcFileStream.is_open()| |!DestFileStream.is_open()) 返回false; 如果(调试模式) { std::cout_C++_File_Class_Input - Fatal编程技术网

C++;调试断言错误 < >我一直在玩C++,决定写一个程序,它涉及到以二进制模式打开和写入文件。我不太熟悉C++的IoSoad功能(我主要是做基于API的编程),但我阅读了几个关于这个主题的技术指导,并编写了一些代码。该代码用于打开一个文件,将其数据读取到缓冲区,然后将该缓冲区转换为另一种格式并写入另一个文件。问题是它不断抛出“调试断言”错误,这显然是围绕无效使用空指针而产生的。然而,当我浏览代码时,我无法理解它。我可能只是误用了iostream库或者犯了一个简单的逻辑错误。我需要有单独的SetMemBlock函数,因为我计划使用相同的基来格式化各种函数上的不同输出。这只是我的原型。无论如何,这里是我的快速n'dirty类设置: const DebugMode = true; class A { public: bool FileFunction( const char *, const char * ); protected: bool SetMemBlock( char *, std::fstream &, std::streamoff & ); private: std::fstream SrcFileStream; std::fstream DestFileStream; }; bool A::SetMemBlock( char* MemBlock, std::fstream & FileStream, std::streamoff & Size ) { std::streamoff TempOff = 0; //This is meant to check for a non-empty buffer and to see if the stream is valid. if( MemBlock != 0 || !FileStream.is_open() ) return false; TempOff = FileStream.tellg(); FileStream.seekg(0, std::ios::end); Size = FileStream.tellg(); MemBlock = new( std::nothrow ) char[ (int) Size ]; if( MemBlock == 0 ) return false; FileStream.seekg(0, std::ios::beg); FileStream.read( MemBlock, (int) Size ); if( !FileStream ) return false; FileStream.seekg(TempOff); return true; } bool A::FileFunction( const char * SrcFile, const char * DestFile ) { char * MemBlock = 0; std::streamoff Size = 0; SrcFileStream.open( SrcFile, std::ios::binary | std::ios::in ); DestFileStream.open( DestFile, std::ios::binary | std::ios::out ); if( !SrcFileStream.is_open() || !DestFileStream.is_open() ) return false; if( DebugMode ) { std::cout<<"Files opened succesfully...\nNow writing memory block..."<<std::endl; } if( !SetMemBlock( MemBlock, SrcFileStream, Size ) ) { std::cout<<"An error occured when reading to memory block!"<<std::endl; return false; } if( DebugMode ) { std::cout<<"Memory block written..."<<std::endl; } DestFileStream.seekp( std::ios::beg ); DestFileStream.write( MemBlock, Size ); SrcFileStream.close(); DestFileStream.close(); delete[] MemBlock; return true; } const DebugMode=true; 甲级 { 公众: 布尔文件函数(常量字符*,常量字符*); 受保护的: bool SetMemBlock(char*,std::fstream&,std::streamoff&); 私人: std::fstream SrcFileStream; std::fstream DestFileStream; }; bool A::SetMemBlock(char*MemBlock,std::fstream和FileStream,std::streamoff和Size) { 标准::streamoff TempOff=0; //这意味着检查非空缓冲区,并查看流是否有效。 if(MemBlock!=0 | |!FileStream.is_open()) 返回false; TempOff=FileStream.tellg(); seekg(0,std::ios::end); Size=FileStream.tellg(); MemBlock=new(std::nothrow)char[(int)Size]; if(MemBlock==0) 返回false; seekg(0,std::ios::beg); read(MemBlock,(int)Size); 如果(!FileStream) 返回false; FileStream.seekg(TempOff); 返回true; } bool A::FileFunction(const char*SrcFile,const char*DestFile) { char*MemBlock=0; 标准::流量大小=0; SrcFileStream.open(SrcFile,std::ios::binary | std::ios::in); DestFileStream.open(DestFile,std::ios::binary | std::ios::out); 如果(!SrcFileStream.is_open()| |!DestFileStream.is_open()) 返回false; 如果(调试模式) { std::cout

C++;调试断言错误 < >我一直在玩C++,决定写一个程序,它涉及到以二进制模式打开和写入文件。我不太熟悉C++的IoSoad功能(我主要是做基于API的编程),但我阅读了几个关于这个主题的技术指导,并编写了一些代码。该代码用于打开一个文件,将其数据读取到缓冲区,然后将该缓冲区转换为另一种格式并写入另一个文件。问题是它不断抛出“调试断言”错误,这显然是围绕无效使用空指针而产生的。然而,当我浏览代码时,我无法理解它。我可能只是误用了iostream库或者犯了一个简单的逻辑错误。我需要有单独的SetMemBlock函数,因为我计划使用相同的基来格式化各种函数上的不同输出。这只是我的原型。无论如何,这里是我的快速n'dirty类设置: const DebugMode = true; class A { public: bool FileFunction( const char *, const char * ); protected: bool SetMemBlock( char *, std::fstream &, std::streamoff & ); private: std::fstream SrcFileStream; std::fstream DestFileStream; }; bool A::SetMemBlock( char* MemBlock, std::fstream & FileStream, std::streamoff & Size ) { std::streamoff TempOff = 0; //This is meant to check for a non-empty buffer and to see if the stream is valid. if( MemBlock != 0 || !FileStream.is_open() ) return false; TempOff = FileStream.tellg(); FileStream.seekg(0, std::ios::end); Size = FileStream.tellg(); MemBlock = new( std::nothrow ) char[ (int) Size ]; if( MemBlock == 0 ) return false; FileStream.seekg(0, std::ios::beg); FileStream.read( MemBlock, (int) Size ); if( !FileStream ) return false; FileStream.seekg(TempOff); return true; } bool A::FileFunction( const char * SrcFile, const char * DestFile ) { char * MemBlock = 0; std::streamoff Size = 0; SrcFileStream.open( SrcFile, std::ios::binary | std::ios::in ); DestFileStream.open( DestFile, std::ios::binary | std::ios::out ); if( !SrcFileStream.is_open() || !DestFileStream.is_open() ) return false; if( DebugMode ) { std::cout<<"Files opened succesfully...\nNow writing memory block..."<<std::endl; } if( !SetMemBlock( MemBlock, SrcFileStream, Size ) ) { std::cout<<"An error occured when reading to memory block!"<<std::endl; return false; } if( DebugMode ) { std::cout<<"Memory block written..."<<std::endl; } DestFileStream.seekp( std::ios::beg ); DestFileStream.write( MemBlock, Size ); SrcFileStream.close(); DestFileStream.close(); delete[] MemBlock; return true; } const DebugMode=true; 甲级 { 公众: 布尔文件函数(常量字符*,常量字符*); 受保护的: bool SetMemBlock(char*,std::fstream&,std::streamoff&); 私人: std::fstream SrcFileStream; std::fstream DestFileStream; }; bool A::SetMemBlock(char*MemBlock,std::fstream和FileStream,std::streamoff和Size) { 标准::streamoff TempOff=0; //这意味着检查非空缓冲区,并查看流是否有效。 if(MemBlock!=0 | |!FileStream.is_open()) 返回false; TempOff=FileStream.tellg(); seekg(0,std::ios::end); Size=FileStream.tellg(); MemBlock=new(std::nothrow)char[(int)Size]; if(MemBlock==0) 返回false; seekg(0,std::ios::beg); read(MemBlock,(int)Size); 如果(!FileStream) 返回false; FileStream.seekg(TempOff); 返回true; } bool A::FileFunction(const char*SrcFile,const char*DestFile) { char*MemBlock=0; 标准::流量大小=0; SrcFileStream.open(SrcFile,std::ios::binary | std::ios::in); DestFileStream.open(DestFile,std::ios::binary | std::ios::out); 如果(!SrcFileStream.is_open()| |!DestFileStream.is_open()) 返回false; 如果(调试模式) { std::cout,c++,file,class,input,C++,File,Class,Input,您正在通过值将MemBlock传递给SetMemBlock。因此,该函数只设置本地副本的值,这对调用函数没有影响;因此调用函数中MemBlock的值仍然是垃圾。将其用作指针可能会导致断言(如果你幸运的话)或者彻底崩溃(如果你不幸运的话)。你想通过引用来传递这个论点 如果您不知道这些术语的含义,请使用谷歌“按值传递”和“按引用传递”。您确实需要了解它们的区别!按引用传递MemBlock: bool A::SetMemBlock( char*& MemBlock, std::fstream

您正在通过值将
MemBlock
传递给
SetMemBlock
。因此,该函数只设置本地副本的值,这对调用函数没有影响;因此调用函数中
MemBlock
的值仍然是垃圾。将其用作指针可能会导致断言(如果你幸运的话)或者彻底崩溃(如果你不幸运的话)。你想通过引用来传递这个论点


如果您不知道这些术语的含义,请使用谷歌“按值传递”和“按引用传递”。您确实需要了解它们的区别!

按引用传递MemBlock:

bool A::SetMemBlock( char*& MemBlock, std::fstream & FileStream, std::streamoff & Size )

哦,罗夫!不,我知道区别。我只是有点迟钝。谢谢你的帮助。