Xref: utzoo comp.sys.ibm.pc:28245 comp.binaries.ibm.pc.d:2873 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!novavax!infocenter!mhoffman From: mhoffman@infocenter.UUCP (Mike Hoffman) Newsgroups: comp.sys.ibm.pc,comp.binaries.ibm.pc.d Subject: Re: HELP need in C (turbo c, fopen problem) Message-ID: <2172@infocenter.UUCP> Date: 2 May 89 13:11:57 GMT References: <1372@naucse.UUCP> Organization: Gould CSD, Fort Lauderdale, FL Lines: 40 in article <1372@naucse.UUCP>, wew@naucse.UUCP (Bill Wilson) says: > > 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; >> } >> > For example to read from the stream, you could use the following: > > while (!feof(in)) > putc(fgetc(fp)); > Maybe I missed something in the translation, but when you fopen() a file with a type argument of "w", the file is either created (if it didn't already exist), or truncated to a length of zero (if it did exist). Therefore, no reads could be done. To open a file for read, use "r"; for reading and writing "r+" To open for append at the end of the file (or create if it doesn't exist) use "a". To truncate or create but allow reads, use "w+". The difference between "r+" and "a" is the position of the stream pointer into the file. ------------------------------------------------------------------------------ Michael J. Hoffman Voice: (407)255-8116 Manufacturing Engineering FAX: (407)255-8186 Encore Computer Corporation Email: mhoffman USnail: 100 N. Babcock St. UUCP: {uunet,codas!novavax,sun,pur-ee}!gould!mhoffman Melbourne, Fl 32935 "Curiouser and Curiouser" -- Alice