Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!ames!apple!motcsd!hpda!hpcuhb!hp-ses!hpdml93!stephen From: stephen@hpdml93.HP.COM (Stephen Holmstead) Newsgroups: comp.sys.amiga Subject: Re: C problems Message-ID: <380023@hpdml93.HP.COM> Date: 30 May 89 15:11:56 GMT References: Organization: HP Disk Memory Division - Boise, ID Lines: 78 >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"? Briefly, open, creat, write, etc. are lower-level I/O routines. Each process 'n' bytes of raw data. fopen, fprintf, fclose, etc. are higher level routines. They will process "formatted" data. For someone new to 'C', I recommend that you use the 'f..' commands (they are easier to produce the desired result but are slower). I assume that you have the complete documentation on these, so I will only mention a few of the command names... fopen() -- opens a file and returns a file handle. fclose() -- closes a file. fprintf() -- just like 'printf' except to a file (formatted print). fputs() -- put a string to a file. fputc() -- put a character to a file. fgets() -- get a string from a file. fgetc() -- get a character from a file. fscanf() -- formatted read (just like 'scanf'). >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]. > >#include >main(argc, argv) ************* PROBLEM: You forgot to tell the compiler the type of the input paramters. The compiler assumed that both were 'int' because you didn't specify otherwise. int argc; char *argv[]; ************* >{ > int c; > FILE *fp1, *fp2; > > if ((fp1=fopen(argv[1], "r"))==NULL) { > printf("Can't open %s", argv[1]); > exit(); > } > > if ((fp2=fopen(argv[2], "w"))==NULL) { > printf("Can't open %s", argv[2]); > exit(); > } > > while ((c=getc(fp1)!=EOF)) > putc(fp2); > >} Just a couple of notes: 1. 'exit' requires a parameter which it returns to the operating system. Most people will return a negative number to indicate an error has occurred. (format: exit(-1);) 2. It is also common practice to make sure that there are at least enough arguments given before you assume they exist: if (argc<3) { printf ("Not enough arguments; format: %s \n", argv[0]); exit(-1); } Otherwise, the code looks good. Have fun with 'C'. name: Stephen Holmstead phone: (208) 323-2840 uucp: hplabs!hpdmlge!stephen uxmail: stephen@hpdmlge usmail: Hewlett-Packard Disk Memory Division 11413 Chinden Blvd Boise, ID 83714