Le 13/08/2011 12:57, Ian Collins a écrit :
> On 08/13/11 09:52 PM, Julien wrote:
>
> Please don't snip attributions, it's rude.
>
Sorry for this. I've got to remember this.
> ...
>>> ...
>> 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?
Sorry, I made a mistake, time_t is not a struct but a datatype.
> Why do you want to do things the C way, rather than the more idiomatic
> C++ forms?
>
In fact, I try to correct cppcheck errors of a file on the internet
which is C style whereas the file has cpp extension.
I replaced the code above by this (the same way of the code you gave
before) :
time_t read_progress() {
time_t stored_secs;
std::ifstream f(CPU_TIME);
if (!f) return 0;
f >> stored_secs;
if (!f) return 0;
else return stored_secs;
}
> fscanf requires you to get the types right, iostreams delegate the task
> to the compiler.
Ok.
Thank you for your help. I'll do other changes to use the C++ style.
Sorry again for having snipped attributions (I never know if i cut too
little or too much)
Julien.
|