Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++ 带有istream_迭代器的程序正在崩溃_C++ - Fatal编程技术网

C++ 带有istream_迭代器的程序正在崩溃

C++ 带有istream_迭代器的程序正在崩溃,c++,C++,我的程序崩溃的原因: #ifndef StreamBuffer_h #define StreamBuffer_h #include <string> #include <fstream> #include <iostream> #include <iterator> using namespace std; enum StreamBufferState { STREAMBUFFER_OK = 0, STREAMBUFFER_EOF =

我的程序崩溃的原因:

#ifndef StreamBuffer_h
#define StreamBuffer_h

#include <string>
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

enum StreamBufferState
{
  STREAMBUFFER_OK = 0,
  STREAMBUFFER_EOF = 1
};

// gzip plik
// type plik | gzip -d
// gzip -d plik.gz
// gzip -dc plik.gz 

class StreamBuffer
{
    istream_iterator<char> iter;
    int maxBufferSize;
    std::string buffer;
 public:
    StreamBuffer(int maxBuffSize, bool streamInput, std::string filename="")
    {
    SetMaxBufferSize(maxBuffSize);
        if(streamInput) // Wejscie strumieniowe
            iter = istream_iterator<char>(std::cin);
        else // Wejscie plikowe
            iter = istream_iterator<char>(fstream(filename.c_str()));
    }
    ~StreamBuffer()
    {
    }
    void SetMaxBufferSize(unsigned int maxBuffSize)
    {
    maxBufferSize = maxBuffSize;
    }
    StreamBufferState FullBufferWithData()
    {
        char c;
        istream_iterator<char> iend;
        for(int i=0;i<maxBufferSize;++i)
        {
            if(iter==iend)
                return STREAMBUFFER_EOF;
            c << *iter;
            buffer += c; // !!!!!! In this line program is crashing down
            iter++;
        }

        return STREAMBUFFER_EOF;
    }
    std::string GetDataBuffer()
    {
    return buffer;
    }
};

#endif
[编辑] 改进代码后,我有:

#ifndef StreamBuffer_h
#define StreamBuffer_h

#include <string>
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

enum StreamBufferState
{
  STREAMBUFFER_OK = 0,
  STREAMBUFFER_EOF = 1
};

// gzip plik
// type plik | gzip -d
// gzip -d plik.gz
// gzip -dc plik.gz 

class StreamBuffer
{
    fstream file;
    istream_iterator<char> iter;
    int maxBufferSize;
    std::string buffer;
 public:
    StreamBuffer(int maxBuffSize, bool streamInput, std::string filename="")
    {
    SetMaxBufferSize(maxBuffSize);
        if(streamInput) // Wejscie strumieniowe
            iter = istream_iterator<char>(std::cin);
        else // Wejscie plikowe
        {
            file.open(filename.c_str(),ios::in);
            iter = istream_iterator<char>(file);
        }
    }
    ~StreamBuffer()
    {
        file.close();
    }
    void SetMaxBufferSize(unsigned int maxBuffSize)
    {
    maxBufferSize = maxBuffSize;
    }
  StreamBufferState FullBufferWithData()
    {
        char c;
        istream_iterator<char> iend;
        for(int i=0;i<maxBufferSize;++i)
        {
            if(iter==iend)
                return STREAMBUFFER_EOF;
            c = *iter;
            buffer += c;
            iter++;
        }

        return STREAMBUFFER_EOF;
    }
    std::string GetDataBuffer()
    {
        string buf = buffer;
        buffer.clear();
        return buf;
    }
};

#endif
\ifndef StreamBuffer\u h
#定义StreamBuffer_h
#包括
#包括
#包括
#包括
使用名称空间std;
枚举流缓冲状态
{
STREAMBUFFER_OK=0,
STREAMBUFFER_EOF=1
};
//gzip-plik
//plik | gzip-d型
//gzip-dplik.gz
//gzip-dc-plik.gz
类流缓冲区
{
流文件;
istream_迭代器iter;
int-maxBufferSize;
字符串缓冲区;
公众:
StreamBuffer(int-maxBuffSize,bool-streamInput,std::string filename=”“)
{
SetMaxBufferSize(maxBuffSize);
if(streamInput)//Wejscie strumeinoiwe
iter=istream_迭代器(std::cin);
else//Wejscie plikowe
{
file.open(filename.c_str(),ios::in);
iter=istream_迭代器(文件);
}
}
~StreamBuffer()
{
file.close();
}
void SetMaxBufferSize(无符号整型maxBuffSize)
{
maxBufferSize=maxBuffSize;
}
StreamBufferState FullBufferWithData()
{
字符c;
istream_迭代器iend;

对于(inti=0;i来说,没有什么可以继续的,但是

您正在使用一个临时的
fstream
),将在完整的 表达式,留下一个悬空引用的
iter

当然,您永远不会初始化
c
(这是未定义的) 但我怀疑这并不是你为什么 撞车。那你怎么办{ myBuffer+=c; } 返回myIStream?STREAMBUFFER_OK:STREAMBUFFER_EOF; } };
除了我真的怀疑这是你想要的:它 将任何错误视为文件结束,当您 部分填充缓冲区,最重要的是,它(如
istream\u迭代器
)跳过空白

如果你能具体说明你想做什么,也许我们
可以提供更多帮助。

为什么要对字符进行二进制移位?您应该了解如何调试像您这样的小程序。istream\u迭代器在解引用后将其分配给c变量会产生垃圾,但我不知道为什么。
c行
c Ok,但是,即使我使fstream成为这个类的成员并使用fstream::open,我在同一个地方也会有相同的错误。您的代码会产生许多编译错误,第一个错误是:错误C2446:':':没有从'std::filebuf'转换到'std::basic_streambuf*'@cppmoster有一个小的打字错误,我已经纠正了。(我每天都使用类似的代码。)好吧,也许我仍然有错误,因为我必须在VS2008中工作,尽管这要感谢我自己管理,代码也是类似的。我仍然使用istream_迭代器。
#ifndef StreamBuffer_h
#define StreamBuffer_h

#include <string>
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

enum StreamBufferState
{
  STREAMBUFFER_OK = 0,
  STREAMBUFFER_EOF = 1
};

// gzip plik
// type plik | gzip -d
// gzip -d plik.gz
// gzip -dc plik.gz 

class StreamBuffer
{
    fstream file;
    istream_iterator<char> iter;
    int maxBufferSize;
    std::string buffer;
 public:
    StreamBuffer(int maxBuffSize, bool streamInput, std::string filename="")
    {
    SetMaxBufferSize(maxBuffSize);
        if(streamInput) // Wejscie strumieniowe
            iter = istream_iterator<char>(std::cin);
        else // Wejscie plikowe
        {
            file.open(filename.c_str(),ios::in);
            iter = istream_iterator<char>(file);
        }
    }
    ~StreamBuffer()
    {
        file.close();
    }
    void SetMaxBufferSize(unsigned int maxBuffSize)
    {
    maxBufferSize = maxBuffSize;
    }
  StreamBufferState FullBufferWithData()
    {
        char c;
        istream_iterator<char> iend;
        for(int i=0;i<maxBufferSize;++i)
        {
            if(iter==iend)
                return STREAMBUFFER_EOF;
            c = *iter;
            buffer += c;
            iter++;
        }

        return STREAMBUFFER_EOF;
    }
    std::string GetDataBuffer()
    {
        string buf = buffer;
        buffer.clear();
        return buf;
    }
};

#endif
class StreamBuffer
{
    std::filebuf myFile;
    std::istream myIStream;
    std::string  myBuffer;
    int myMaxBufferSize;
public:
    StreamBuffer(int maxBufferSize, std::string const& filename )
        : myIStream( filename.empty() ? std::cin.rdbuf() : &myFile )
        , myMaxBufferSize( maxBufferSize )
    {
        if ( ! filename.empty() ) {
            myFile.open( filename.c_str() );
            if ( ! myFile.is_open() ) {
                //  throw ?
            }
        }
    }
    StreamBufferState FillBufferWithData()
    {
        char c;
        while ( myBuffer.size() < myMaxBufferSize && myIStream >> c ) {
            myBuffer += c;
        }
        return myIStream ? STREAMBUFFER_OK : STREAMBUFFER_EOF;
    }
};