Xref: utzoo comp.sys.ibm.pc:27882 comp.binaries.ibm.pc.d:2802 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!harmonica.cis.ohio-state.edu!schanck From: schanck@harmonica.cis.ohio-state.edu (Christopher Schanck) Newsgroups: comp.sys.ibm.pc,comp.binaries.ibm.pc.d Subject: Re: HELP need in C (turbo c, fopen problem) Message-ID: <45275@tut.cis.ohio-state.edu> Date: 25 Apr 89 19:38:57 GMT References: <353@h.cs.wvu.wvnet.edu> <1372@naucse.UUCP> Sender: news@tut.cis.ohio-state.edu Reply-To: Christopher Schanck Followup-To: comp.sys.ibm.pc Organization: Ohio State University Computer and Information Science Lines: 46 In article <1372@naucse.UUCP> wew@naucse.UUCP (Bill Wilson) writes: >From article <353@h.cs.wvu.wvnet.edu>, by packer@a.cs.wvu.wvnet.edu (Michael A Packer): >> >> i am trying to open a file. When i open the file my filepointer is >> being returned (not null). When i look at what fp is pointing to >> there is garbage in the "buffer" and "curp" as declared by FILE. >> >> if ((fp=fopen("junk.dat","w"))==NULL) >> { >> printf ("Can't open file\n"); >> return; >> } >> >fopen simply opens a file and associates a "stream" with it. The >pointer may then be used by other functions that use a stream to operate. >For example to read from the stream, you could use the following: > >while (!feof(in)) > putc(fgetc(fp)); > >The file pointer is only used by the functions that work with streams >and are not in essence an actual pointer into the file as I understand >it. You will need to use the fread or fget function to read from the >file and there are equivalent functions to write. This jives with my understanding of the FILE *fp declaration. As I understand it, fp simply points to a structure which is used by stream IO functions to manipulate the file. When you do the fopen call, it allocates the space needed for the FILE structure. You, the programmer, are not actually supposed to directly manipulate the FILE structure. Use the stream IO function (fgets,fscanf,fprintf, etc...) to manipulate files opened in this way. On the other hand, if you want direct control, use the read (_read) function and the write (_write) functions in conjunction with the open (_open) and close (_close) functions to directly manipulate files. Hope this helps, Chris -=- "Does the phrase 'go suck a lemon' hold any meaning for you?" ---- Raphael, in "TMNT: The Epic Begins" Christopher Schanck (schanck@cis.ohio-state.edu)