C problems

pat at mirror.TMC.COM pat at mirror.TMC.COM
Thu Jun 1 23:56:00 AEST 1989


/* Written  9:40 pm  May 29, 1989 by armhold at topaz.rutgers.edu in mirror:comp.lang.c */
/* ---------- "C problems" ---------- */

>I started using my C compiler today (Manx Aztec V3.6a), and I'm really
>frustrated with I/O. I have the K&R book, _Understanding C_ by Bruce
>Hunter, as well as the documentation that came with Manx. All three of
>them left me bewildered with the various file I/O conventions. There's
>open, fopen, creat, etc... Which do I use when? What about writing to
>the console? Can anyone recommend a GOOD book that explains the
>various methods of I/O and also points out differences between
>"standard" and "machine specific"? 

What I think that you should do, since it sounds like you are still
learning, is to use the higher level functions like fopen(), fclose(),
fscanf(), and such.  These functions take care of the needs for the 
I/O stream (with buffer allocation and such) for you and in that way
while you are learning you don't have to be bogged down with the 
sordid details of I/O.  As you progress and understand how the pointers
work and how to safely allocate memory and other needed things, then
you can get into lower level I/O with open, write, and things along
that nature.  If you are learning, be patient, those pointers can be
a b***h!


Also:

>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].

Whenever you define a function (and main() IS a function) you must declare
the types of the arguments.  If you don't then the compiler will (at best)
default them to integers.  This is where you get "too many subscripts or
indirection on integer."

Here's how you correct it:

#include <stdio.h>
main(argc, argv)
int argc;
char **argv;      /* since you are learning it may be easier to understand
                     this syntax better by defining it as:  char *argv[]  */
{
	int c;
	FILE *fp1, *fp2;      ___subscript on an integer before fix.
		       	     /
	if ((fp1=fopen(argv[1], "r"))==NULL) {
		printf("Can't open %s", argv[1]);
		exit();
		}

	.
	.
	.

}


Good luck, George.



More information about the Comp.lang.c mailing list