Re: ifstream::get() surprise
From: Jacek Dziedzic (jacek__NOSPAM___at_janowo.net)
Date: 08/27/04
- Next message: Siemel Naran: "Re: generating strings"
- Previous message: Victor Bazarov: "Re: Problem with template using base class template in GCC"
- In reply to: Mike Wahler: "Re: ifstream::get() surprise"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 27 Aug 2004 06:05:47 +0200
Mike Wahler wrote:
>>
>>int main() {
>> ifstream in("test.txt");
>
> You need to check here whether the file was opened
> successfully or not, and not proceed if it wasn't.
Obviously, but not in the famous "shortest possible program
displaying the behaviour", right?
> Also note that 'get()' is an unformatted input function.
> Using it with a text-mode stream (the default) could have
> unexpected results.
What unexpected results, could you clarify?
> If you want to read unformatted,
> open with 'std::ios::binary'.
Nope, I want to read formatted. Binary mode is out because
I don't want to mind CR/LF differences. Plus I'm reading
trivial configuration text files.
> But I think you're probably
> just using the wrong function. See below.
>
So what's wrong with get()?
>
> It's not surprising when you read the specification of 'std::istream::get()'
>
Yes, but I don't have these.
> No such thing as 'successful read of zero characters.' If characters
> were requested and none were extracted, that's a 'failure'.
I see that now, but it's not obvious a'priori. Some read functions
don't complain about reading zero bytes or seek functions don't
complain about seeking zero bytes. I mean it's obvious for someone
who KNOWS already, but for me it was counter-intuitive.
> ============== begin quote ===========================
> ISO/IEC 14882:1998(E)
>
> 27.6.1.3 Unformatted input functions
>
> basic_istream<charT,traits>& get(char_type* s, streamsize n,
> char_type delim );
>
> 7 Effects: Extracts characters and stores them into successive locations
> of an array whose first element is designated by s. (286) Characters
> are extracted and stored until any of the following occurs:
>
> -- n 1 characters are stored;
>
> -- endoffile occurs on the input sequence (in which case the function
> calls setstate(eofbit));
>
> -- c == delim for the next available input character c(in which case c
> is not extracted).
>
> 8 If the function stores no characters, it calls setstate(failbit) (which
> may throw ios_base::failure (27.4.4.3)). In any case, it then stores a
> null character into the next successive location of the array.
>
> 9 Returns: *this.
>
> basic_istream<charT,traits>& get(char_type* s, streamsize n)
>
> 10 Effects: Calls get(s,n,widen('\n'))
>
> 11 Returns: Value returned by the call.
> ============== end quote ===========================
>
Yes, that was helpful.
> I recommend you eschew the array and use std::strings and
> std::getline to parse your file.
>
> std::string s;
> while(std::getline(in, s))
> cout << s << '\n';
>
> if(!in.eof())
> cerr << "Error reading\n";
>
> Now you don't have to worry if your array is big enough,
> and you can get at individual characters the same way
> as from an array, e.g.
>
> char c = s[0];
You overdid that one a bit, Mike :). I am in the middle of
coding a 'better_ifstream' class which inherits from ifstream
and supplies facilities like get_string(), get_word(),
parse_phrase(), etc.
std::strings are useless in my problem, since I need
byte-copiable POD types to be transferred across different
processors in a parallel system. But you couldn't have known that,
since the rule is to post the shortest code suffering from the
questioned behaviour, not the *neatest* shortest code, right? :)
> HTH,
> -Mike
yes, the quote did,
- J.
- Next message: Siemel Naran: "Re: generating strings"
- Previous message: Victor Bazarov: "Re: Problem with template using base class template in GCC"
- In reply to: Mike Wahler: "Re: ifstream::get() surprise"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|