C problems

Doug Merritt doug at xdos.UUCP
Wed May 31 12:16:09 AEST 1989


In article <May.29.21.40.18.1989.8866 at topaz.rutgers.edu> armhold at topaz.rutgers.edu (George Armhold) writes:
>
>Also, here's some source I tried today. What I'm trying to do is
>simply copy one file to another. When I compile it, I get error 125
>which means "too many subscripts or indirection on integer". It occurs
>wherever I mention argv[x].

Oh well, I may as well join the hundreds of others who will all point out
the same problems. I usually resist but I feel like burning bandwidth
for once:

>main(argc, argv)
>{

You need to declare the parameters here, like for instance:
	main(argc, argv)
		int	argc;
		char	**argv;

>	while ((c=getc(fp1)!=EOF)) 
>		putc(fp2);

There are two problems here...one is that the while() is misparenthesized,
it should be:
>	while ((c=getc(fp1)) != EOF) 

As written it was doing (c = (getc(fp1) != EOF)), which always assigns
either a zero or a one to 'c'.

Second, you're not giving the right parameters, 'c' is missing from
the call:
		putc(c, fp2);

These are all pretty obvious bugs, so I suspect that your original
frustration in not being sure which functions to use in the first place
became a problem in itself. Pirsig called this a "gumption trap" in
"Zen and the Art of Motorcycle Maintenence". The recommended cure is
to stop, cool off, relax, and then go back to what you were doing.

For various reasons (style, portability, forming bug-avoiding habits)
you should also close the files before exiting, and also call exit
explicitly:
	fclose(fp1);
	fclose(fp2);
	exit(0);

The explicit exit provides a clean return code so that your programs
could be used in your 'make' scripts.

For the same reason, your error exits should give a non-zero exit code:
	if (...open fails...)
		exit(1);
Or on the Amiga, perhaps:
		exit(10);
	
Programming in C is fun once you get used to it.
	Doug
-- 
Doug Merritt		{pyramid,apple}!xdos!doug
Member, Crusaders for a Better Tomorrow		Professional Wildeyed Visionary

"Welcome to Mars; now go home!" (Seen on a bumper sticker off Phobos)



More information about the Comp.lang.c mailing list