Re: Visual C++ 2005 basic_istream::get(buf, size, delim) bug?
kanze <kanze@gabi-soft.fr> wrote:
Terry G wrote:
static char Line[1024];
while (std::cin.get(Line, sizeof(Line), '\n')) {
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
// Process line.
}
James Kanze wrote:
What happens if the program encounters a line of more than 1023
characters?
My intent was to only process the first 1023 characters and
then ignore the rest.
Which is exactly what the code does; I understand that. I was
responding to the claim that ignore would never ignore more than
one character, not questionning your code.
another approach is to create a class holding all the needed
data [except stream &] and creating an operator >> () for the class
doing exzctly what you want with streambuf functions, setting eof and
fail upon actual reading of 0 chars.
// line_reader.h
#ifndef LINE_READER_H_
#define LINE_READER_H_
#include <iosfwd>
class line_reader
{
char *buf;
std::size_t len;
char delim;
public:
line_reader(char *a,std::size_t b,char
c):buf(a),len(b),delim(c){--len;}
std::istream & do_read(std::istream &in);
};
inline std::istream & operator >> (std::istream &in,line_reader &line)
{
return line.do_read(in);
}
#endif
// line_reader.cp
#include "line_reader.h"
#include <streambuf>
#include <istream>
std::istream & line_reader::do_read(std::istream &in)
{
if(!in)
return in;
std::streambuf *sb = in.rdbuf();
char *p = buf;
char *q = buf +len;
int c;
while((c=sb->sbumpc())!=EOF && p!=q && c!=delim)
{
*p++ = c;
}
*p = '\0';
if(p==q)
{
while((c=sb->sbumpc())!=EOF && c!=delim) {}
}
if(c==EOF)
{
in.setstate(std::ios_base::eofbit|std::ios_base::failbit);
}
return in;
}
a little 'low level' perhaps but it reads upto size-1 chars or a until a
delim is found which ever occurs first, if size-1 chars are read without
a '\n' it eats everything upto and including the next delim unless it
hits end of file...
usuage is simple
char buf[1024];
line_reader reader(buf,1024,'\n');
while(in_stream >> reader)
process(buf);
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]