Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8779

Re: What's the most secure way to read a long int ?

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: What's the most secure way to read a long int ?
Date 2011-08-13 22:57 +1200
Message-ID <9an3kiFdeuU1@mid.individual.net> (permalink)
References <4e463fcc$0$32662$426a74cc@news.free.fr> <9amtu7FdetU2@mid.individual.net> <4e46491d$0$23660$426a74cc@news.free.fr>

Show all headers | View raw


On 08/13/11 09:52 PM, Julien wrote:

Please don't snip attributions, it's rude.

 > I wrote:

>> Do it the C++ way:
>>
>> std::ifstream f( CPU_TIME );
>> if (!f) return;
>>
>> long c;
>> f>>  c;
>>
>> if(!f) return;
>>
> Ok, cppcheck is mute about this now.
>
> What about for this kind of code ?

It's horrible...

> time_t read_progress() {
>       time_t stored_secs;
>       FILE* f = fopen(PROGRESS_FN, "r");
>       if (!f) return(0);
>       int n = fscanf(f, "%ld",&stored_secs);
>       fclose(f);
>       if (n != 1) return(0);
>       else return(stored_secs);
> }
>
> It's quite the same except the variable is a struct. So f>>  stored_secs
>    wouldn't work here.

Which variable is a struct?

Why do you want to do things the C way, rather than the more idiomatic 
C++ forms?

fscanf requires you to get the types right, iostreams delegate the task 
to the compiler.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What's the most secure way to read a long int ? Julien <serval2412@yahoo.fr> - 2011-08-13 11:12 +0200
  Re: What's the most secure way to read a long int ? Ian Collins <ian-news@hotmail.com> - 2011-08-13 21:20 +1200
    Re: What's the most secure way to read a long int ? Julien <serval2412@yahoo.fr> - 2011-08-13 11:52 +0200
      Re: What's the most secure way to read a long int ? Ian Collins <ian-news@hotmail.com> - 2011-08-13 22:57 +1200
        Re: What's the most secure way to read a long int ? Julien <serval2412@yahoo.fr> - 2011-08-13 13:46 +0200
        Re: What's the most secure way to read a long int ? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-14 05:58 +0000

csiph-web