|
From: Per W. <pw...@ia...> - 2003-07-03 20:28:20
|
Oops, reversed logic of last sentence. It should of course say "wasn't
intended".
On Thu, 3 Jul 2003, Per Westermark wrote:
> Sorry for coming out a bit rough but it's just that I think this issues
> are important. Too many programs fails miserably because of this kind of
> errors.
>
> First of all - count the number of parameters used to
> sprintf(temp,"\r\n","");
> What would you say the third parameter was used for. Sending extra
> parameters just because a variadric function allows it MUST be called
> inefficient.
>
> Second - do you notice any formatting character in the second parameter?
> If not, then there is no need to call sprintf(), and strcpy() would
> clearly be preferable to use.
>
> Third - why talk about how expensive a line is, when - in reality - it
> isn't needed at all. You might notice that
> WriteFile(handle,"\r\n",2,&dwCount,NULL);
> doesn't need the text to becopied to a temporary buffer before it is
> emitted. Also, 2 is the correct number of characters to emit if the goal
> is to generate a normal text file - at least until we aim for Unicode or
> multi-byte encodings.
>
> I'm pretty sure that temp wasn't declared as:
> char temp[2]
> so sizeof(temp) will most certainly result in an incorrect number of
> characters being written.
>
> Notice that
> sprintf(temp,"\r\n");
> would require 3 characters in temp, while
> sprintf(temp,"%3d",x);
> would require at least 4 - depending on the value of x.
>
> Obviously, it isn't possible to emit the two strings to a text file
> by using sizeof(temp). It wasn't possible in the original question either,
> since sprintf() requires room an extra termination chaarcter. Either
> temp[] would be large enough for that character - but then the termination
> character would also be emitted. Or temp[] was just large enough for the
> characters, but then sprintf() would have made a buffer overflow.
>
> But still - the code didn't prove that -99 <= ran1[loop] <= 999. Without
> that assumption, it isn't possible to predict the number of charactesr
> that sprintf() needs when performing the %3d processing. Given a variable
> number of characters in the formatted string, sizeof(temp) may never work,
> and strlen(temp) must be used.
>
> If emitting a binary file, sizeof(temp) is ok, since then temp[] might be
> defined "large enough", and the program may safely ignore any random
> characters emitted after the '\0' string termination and the full size of
> the temp[] buffer. However, the use of "\r\n" in the output seems like a
> very large indication that a binary fixed record-size file was intended.
>
> /Per W
>
> On Thu, 3 Jul 2003, Daniel Glenfield wrote:
>
> > Given the speed of processors now, something like that can hardly be called
> > expensive can it?
> >
> >
> > >From: Per Westermark <pw...@ia...>
> > >To: Daniel Glenfield <sp...@ho...>
> > >CC: jab...@ma...,
> > ><dev...@li...>
> > >Subject: Re: [Dev-C++] (no subject)
> > >Date: Thu, 3 Jul 2003 19:17:12 +0200 (CEST)
> > >
> > >But that ignores the problem that sizeof(temp) doesn't take into account
> > >the number of characters in the buffer, only total declared size of it. As
> > >such, sizeof(temp) should never be used for emitting text files unless you
> > >really, really, really - did I say really? - know what you are doing.
> > >strlen() is available specifically for checking the length of a string.
> > >
> > >In the first WriteFile() call, temp contains 2 characters, a terminating
> > >zero and an unknown number of extra garbage at the end - depending on what
> > >size it was declared as.
> > >
> > >In the second WriteFile() call, temp constans 3 characters, a terminating
> > >zero and an unknown number of extra garbage.
> > >
> > >Besides, sprintf(temp,"\r\n","") is a bit expensive since the formatting
> > >string didn't contain any formatting. That is when strcpy(temp,"\r\n")
> > >should be used.
> > >
> > >Further, since the emitted string is a constant, why not directly emit
> > >WriteFile(hFile,"\r\n",2,&dWritten,NULL);
> > >
> > >Any why not check the return value and the value of dWritten? And - just
> > >a bit more knit-picking - dWritten should be called dwWritten since it is
> > >of type double-word.
> > >
> > >/Per W
> > >
> > >On Thu, 3 Jul 2003, Daniel Glenfield wrote:
> > >
> > > > Hey you guys hear that? I was correct!!! Ha ha! This feels great!
> > > >
> > > > Cheers,
> > > > Daniel
> > > >
> > > > >From: "Jorge Abramian" <jab...@ma...>
> > > > >Reply-To: <jab...@ma...>
> > > > >To: <dev...@li...>
> > > > >Subject: [Dev-C++] (no subject)
> > > > >Date: Thu, 3 Jul 2003 09:47:14 -0400
> > > > >
> > > > >Hey guys, thanks for the help,
> > > > >
> > > > >Actually Daniel Glenfield was write. I did not try Per W. solution that
> > > > >probably would work either.
> > > > >
> > > > >I changed the code to the followin and it worked well:
> > > > >
> > > > >/////////
> > > > > for (loop=0;loop<Number_of_Points;loop++)
> > > > > {
> > > > > DWORD dWritten;
> > > > > sprintf (temp,"\r\n","");
> > > > > WriteFile(hFile, temp, sizeof(temp),&dWritten, NULL);
> > > > > sprintf (temp,"%3d",ran1[loop]);
> > > > > WriteFile(hFile, temp, sizeof(temp),&dWritten, NULL);
> > > > > }
> > > > >/////////
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >-------------------------------------------------------
> > > > >This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> > > > >Data Reports, E-commerce, Portals, and Forums are available now.
> > > > >Download today and enter to win an XBOX or Visual Studio .NET.
> > > >
> > > >http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
> > > > >_______________________________________________
> > > > >Dev-cpp-users mailing list
> > > > >Dev...@li...
> > > > >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > > > >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > > >
> > > > _________________________________________________________________
> > > > Sign-up for a FREE BT Broadband connection today!
> > > > http://www.msn.co.uk/specials/btbroadband
> > > >
> > > >
> > > >
> > > > -------------------------------------------------------
> > > > This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> > > > Data Reports, E-commerce, Portals, and Forums are available now.
> > > > Download today and enter to win an XBOX or Visual Studio .NET.
> > > > http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
> > > > _______________________________________________
> > > > Dev-cpp-users mailing list
> > > > Dev...@li...
> > > > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > > >
> > >
> >
> > _________________________________________________________________
> > Express yourself with cool emoticons - download MSN Messenger today!
> > http://www.msn.co.uk/messenger
> >
> >
> >
> > -------------------------------------------------------
> > This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> > Data Reports, E-commerce, Portals, and Forums are available now.
> > Download today and enter to win an XBOX or Visual Studio .NET.
> > http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|