amy
amy

Reputation: 1

How to use ifstream of C++ to read from a file using length of the data rather than new line?

Instead of the ifstream >> terminating reading for a file whenever it finds newline, I would like to read from a file using data size. Is there a way to do this?

Currently I use mystream.read(string, length), but I would like to used the ">>" operator instead of read.

Upvotes: 0

Views: 768

Answers (5)

André Caron
André Caron

Reputation: 45224

If you really just want the client code to use operator>>, you can write an operator that calls .read() by using an intermediate type.

class Buffer
{
    char * myData;
    std::size_t mySize;
public:
    Buffer ( char * data, std::size_t size )
      : myData(data), mySize(size)
    {}
    friend operator>> ( std::istream& stream, Buffer& buffer )
    {
        return stream.read(buffer.myData, buffer.mySize);
    }
};

This allows you to write:

static const std::size_t size = 100;
char data[size];
std::cin >> Buffer(data, size);

This can come in useful if you know you'll be reading multiple parts with known lengths, such as in:

static const std::size_t header_size =  5;
static const std::size_t stream_size = 20;
static const std::size_t footer_size = 12;
char header[header_size];
char stream[stream_size];
char footer[footer_size];
std::cin >> Buffer(header, header_size)
         >> Buffer(stream, stream_size)
         >> Buffer(footer, footer_size);

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

.read is the way to do what you're asking.

It doesn't make a huge amount of sense to want to do it with operator>> instead, which simply does not do this.

Just use .read, and move on.


Update Either that, or I misread your question and Kris is right.

Upvotes: 2

BrandonSun
BrandonSun

Reputation: 129

Well, consider the following if the syntax does not confuse you:

char g_buf[MAX_BUFFER_LEN];

// The operator>> does not stop reading until it hits the end of file or the data read equals to specified cnt
istream& operator>>(istream& in, int cnt)
{
    return in.getline(g_buf, cnt, char(6));
}

int main()
{
    ifstream in("e:/a.txt");

    // Reads 200 chars from file and put them in g_buf
    in>>200;

    cout<<g_buf<<endl;

    in.close();
}

Upvotes: 0

jbruni
jbruni

Reputation: 1247

operator>> only takes one parameter -- a reference to the destination. Without being able to pass in a length somewhere, you really don't have a choice but to use the .read() method.

Maybe you could override operator>> and pass in a reference your own special class that embeds a length parameter.

This seems like a lot of work just to use the >> syntax.

Upvotes: 0

Kris
Kris

Reputation: 1840

Something like this? This reads it into a std::vector.

inline void util::operator>>(const std::string &address, std::vector<std::string> &v) throw()
{
    std::ifstream fin;
    fin.open(address.c_str());
    for(std::string s; std::getline(fin, s, '\n'); ) {
        v.push_back(s);}
}

Though, that's some old code I wrote about 5 years ago. Not sure if that's a best practice but could, perhaps, be helpful? Ignore me if I mis-understood :)

Upvotes: 0

Related Questions