해당 클래스에서 일부 System.IO 함수를 다시 만들었습니다. 버퍼를 설정하고 n 바이트의 바이트를 할당하면 해당 바이트가 읽히고 해당 버퍼의 끝 부분에 임의의 바이트가 추가됩니다. 예를 들어버퍼 끝에 무작위 바이트가 추가되었습니다.
:
는내 홈페이지 :
int main(int argc, char *args[])
{
SetConsoleTitle(TEXT("Stream Test."));
cout<<"Press any Key to begin reading.";
cin.get();
const char* data = File::ReadAllBytes(args[1]);
Stream* stream = new Stream(data);
char* magic = new char[8];
stream->Read(magic, 0, 8);
magic[8] = '\0';
cout<<magic<<endl<<endl;
delete[]data;
cout<<"Press any key to quit.";
cin.get();
return 0;
}
여기 내 시스템이다 :: IO 네임 스페이스 + 스트림 클래스 :
namespace System
{
namespace IO
{
class File
{
public:
static char* ReadAllBytes(const char *name)
{
ifstream fl(name, ifstream::in|ifstream::binary);
fl.seekg(0, ifstream::end);
size_t len = fl.tellg();
char* ret = new char[len+1];
ret[len] = '\0';
fl.seekg(0);
fl.read(ret, len);
fl.close();
return ret;
}
//not sure of this use yet.
static size_t fileSize(const char* filename)
{
ifstream in(filename, ifstream::in | ifstream::binary);
in.seekg(0, ifstream::end);
return in.tellg();
}
};
class Stream
{
public:
const char *_buffer;
__int64 _origin;
__int64 _position;
__int64 _length;
__int64 _capacity;
bool _expandable;
bool _writable;
bool _exposable;
bool _isOpen;
static const int MemStreamMaxLength = 2147483647;
Stream()
{
InitializeInstanceFields();
}
Stream(const char *buffer)
{
_buffer = buffer;
_length = strlen(_buffer);
_capacity = _length;
_position = 0;
_origin = 0;
_expandable = false;
_writable = true;
_exposable = true;
_isOpen = true;
}
int ReadByte()
{
if (_position >= _length)
return -1;
return _buffer[_position++];
}
void Read(char* &buffer, int offset, int length)
{
if((_position + offset + length) <= _length)
{
memcpy(buffer, _buffer + (_position + offset), length);
_position += length;
}
}
private:
void InitializeInstanceFields()
{
_origin = 0;
_position = 0;
_length = 0;
_capacity = 0;
_expandable = false;
_writable = false;
_exposable = false;
_isOpen = false;
}
};
}
}
이 일어나고 끝나는 것입니다 :
왜이 사람이 설명 할 수 있습니까? appens, 어떻게 고칠 수 있습니까? 나는 C++을 처음 사용하기 때문에 설명이 도움이 될 것입니다. 또한 내 스크립팅을 비판하지 마세요, 나쁘고, 구식이 될지도 모르는 등 알고 있습니다.하지만 배우는 것에 익숙하며 도움을주는 조언이 더 좋습니다. :)
Null 터미네이터가 누락 되었습니까? 'magic [7] = '\ 0';'. –
@JesseGood 해보겠습니다. – MysteryDev
@JesseGood 와우 내가 '= 0 /'하고 있었다 !! 고마워요.하지만 처음부터 왜해야만하는지 설명 할 수 있습니까? 나는 어딘가에 창문이 당신이 말한 공간이나 그 주변의 공간을 항상 할당하지는 않는다는 것을 읽었다. – MysteryDev