0

我正在从该类中重新创建一些 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;
}

这是我的 System::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;
                }
        };
    }
}

这就是最终发生的事情: 怎么了

谁能解释为什么会发生这种情况,我该如何解决,或者其他什么?我是 C++ 新手,所以任何解释都会有所帮助。另外请不要批评我的脚本,我知道它可能很糟糕、过时、已弃用等,但我愿意学习,任何有帮助的建议都会变得更好。:)

4

2 回答 2

0

您只能operator << (char *)在 C 风格的字符串上使用,而不是任意的字符数组。您如何期望它知道要输出多少个字符?

于 2013-02-12T03:51:17.147 回答
0

我猜该文件没有正确打开,因此根本没有设置魔法缓冲区,这留下了初始化的垃圾数据:

如果构造函数没有成功打开文件,尽管没有文件与流缓冲区关联并且设置了流的失败位(可以通过继承成员失败检查),但仍会创建对象。 http://www.cplusplus.com/reference/fstream/ifstream/ifstream/

尝试在此过程中添加更多错误检查(使用 cout),尤其是在打开和读取缓冲区时。也许将魔术缓冲区设置为零或成功时被覆盖的可识别的东西。

于 2013-02-12T04:01:49.497 回答