tags:

views:

72

answers:

2

I'm having problems with std::getline on Mac OSX Snow Leopard. For some reason it limit the size of the input, while on Debian/Ubuntu it's unlimited size?

std::getline(std::cin, input)

Any clues about the limit?

+1  A: 

It is not the std::getline that differs between the platform (Windows/Unix/Mac OSX). It's how the line terminators are defined over different platforms. It is '\n\r' for Windows-based, '\n' for unix-based and ('\r'?) for MacOS platforms. Try generating your text file (or a test file) using a MacOS editor and examine if you still have a limit in the size of the input using std::getline?

Pirooz
@Pirooz: Your general answer is good, but Macs do not use `\r` as a delimiter. They use `\n` like any Unix system. (In extremely old versions of Mac OS, prior to 2001, you might find `\r` used.)
Nate
On Windows systems, `\r\n` is used, not `\n\r`. In any case, if the file is opened in text mode, it doesn't matter: the line ending will be normalized to be `\n` regardless of what the platform line ending is.
James McNellis
+1  A: 

The C++ standard says this about the getline function:


21.3.7.9 Inserters and extractors [lib.string.io]

template<class charT, class traits, class Allocator>
  basic_istream<charT,traits>&
    getline(basic_istream<charT,traits>& is,
            basic_string<charT,traits,Allocator>& str,
            charT delim);

Effects: Begins by constructing a sentry object k as if by basic_istream<charT,traits>::sentry k(is). If bool(k) is true, it calls str.erase() and then extracts characters from is and appends them to str as if by calling str.append(1,c) until any of the following occurs:

  • end-of-file occurs on the input sequence (in which case, the getline function calls is.setstate(ios_base::eofbit)).
  • c == delim for the next available input character c (in which case, c is extracted but not appended) (27.4.5.3)
  • str.max_size() characters are stored (in which case, the function calls is.setstate(ios_base::failbit)) (27.4.5.3)

So, the only reasons why you would be getting a short line are (a) your C++ implementation does not conform to the standard (highly unlikely), you're hitting end-of-file, or you're reading a 4 gigabyte line (highly unlikely).

Adam Rosenfield
Mac OS X builds 64-bit by default, so 4 gigabytes should not be the `string::max_size()` limit.
Potatoswatter