help!!!

John Nagle nagle at animats.com
Thu Oct 7 07:17:32 CEST 2010


On 10/6/2010 8:02 PM, Seebs wrote:
> On 2010-10-06,<fkrafi at aboutrafi.net23.net>  <fkrafi at aboutrafi.net23.net>  wrote:
>> plz can u convert this cpp file into python i need that badly as soon as
>> possible... I am new to python. I just wanna learn it....
>
> Having come to realize that this is a homework problem, I would of course
> be glad to.
>
> The original program:
>
> 	#include<cstdio>
>
> 	int main()
> 	{
> 	    int a[100], n;
> 	    freopen("input.txt", "r", stdin);
> 	    scanf("%d",&n);
> 	    for(int i=1; i<=n; i++)
> 		scanf("%d",&a[i]);
> 	    return 0;
> 	}
>

    This guy doesn't know C very well, either.

    First, "scanf" was deprecated over five years ago.
And reopening "stdin" is rarely done, although
it is possible.

    Second, there's a buffer overflow.  "n" is obtained
from external input, then used to control the "for"
statement. If N is >= 99, there will be a buffer
overflow on "a".  Also, starting to fill "a" from 1
looks like an error.

    Third, the program doesn't actually do anything.
There's no output.

It looks like this approach to C came from

http://beej.us/guide/bgc/output/html/multipage/freopen.html

					John Nagle



More information about the Python-list mailing list