[comp.binaries.ibm.pc.d] HELP need in C

packer@a.cs.wvu.wvnet.edu (Michael A Packer) (04/25/89)

	I am having a problem with Turbo C (2.0) that I am hoping
someone else has already encountered and can help me with.

problem:

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.

i have not had this problem anywhere else (ie: in any other projects)
just the one i am currently working on.  I am running on a Zenith 286
with 512K, i believe it to be a problem with the amount of memory
i have on my machine.  

my open statement is normal:

if ((fp=fopen("junk.dat","w"))==NULL)
 {
  printf ("Can't open file\n");
  return;
 }

when i examine *fp right after that statement is when it is all messed up!
Anyone that can offer any help would be greatly appreciated.  

I really need to have this solve in the next day and can find no other
way to get help then other NETters, I tried to call BORLAND but was
placed on infinite hold.

please respond to packer@a.cs.wvu.wvnet.edu (wrong in header line)

or un027661@wvnvaxb.wvnet.edu

thanks SO much in advance!!!!!!!!

michael packer
USMAIL:	318 Knapp Hall, WVU, Morgantown, WV 26506
PHONE:	304 293-3607
INTERNET : packer@a.cs.wvu.wvnet.edu
USENET:  {allegra,bellcore,ihpn4!cadre,decvax!idis,psuvax1}!pitt!wvucsa!packer

wew@naucse.UUCP (Bill Wilson) (04/26/89)

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.  

The manual (if you have one) shows how to work with the function.
It even gives an example in the 2.0 manual for making a copy of a
file on page 135 (Reference Guide).  

Good Luck.
 
-- 
Bill Wilson                          (Bitnet: ucc2wew@nauvm)
Northern AZ Univ
Flagstaff, AZ 86011
{He's Watching me Watching you Watching him...}

schanck@harmonica.cis.ohio-state.edu (Christopher Schanck) (04/26/89)

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)

kevinf@infmx.UUCP (Kevin Franden) (04/26/89)

(My mailer got as far as .ames and barfed.  Sorry to put it here...)

I'd check to see that:
		a) The file DOES in fact exist.
				and
		b) You havn't exceeded the # of allowed file handles 
		   for DOS.  (in config.sys the line that says FILES=??  
		   Most people have theirs around 15; I set mine to 25 
		   'cause I _hate_ bugs like this.)
		
		   By default you get 5 but those are taken up by:
		   stdin, stdout ,stderr and the program you're running 
		   (possibly some others that I can't think of) but I 
		   have mine set higher so I don't care.






-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Kevin Franden		    UUCP: {backbone}!pyramid!infmx!kevinf
Informix Software Inc
disclaimer("I said what I said and not my employer");
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

mju@mudos.ann-arbor.mi.us (Marc Unangst) (04/26/89)

In article <1268@infmx.UUCP>, kevinf@infmx.UUCP (Kevin Franden) writes:

 >                   By default you get 5 but those are taken up by:
 >                   stdin, stdout ,stderr and the program you're running 
 >                   (possibly some others that I can't think of) but I 
 >                   have mine set higher so I don't care.

Default file handles are:

        stdin   [0]
        stdout  [1]
        stderr  [2]
        stdaux  [3]
        stdprn  [4]

It's gotta assign more than 5; if it didn't, no program could ever open
a file!  (10, maybe?  I don't know...)


--  
Marc Unangst
UUCP smart    : mju@mudos.ann-arbor.mi.us
UUCP dumb     : ...!uunet!sharkey!mudos!mju
UUCP dumb alt.: ...!{ames,rutgers}!mailrus!clip!mudos!mju
Internet      : mju%mudos@cardiology.ummc.umich.edu

Ralf.Brown@B.GP.CS.CMU.EDU (04/27/89)

In article <313.24554F24@mudos.ann-arbor.mi.us>, mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:
}In article <1268@infmx.UUCP>, kevinf@infmx.UUCP (Kevin Franden) writes:
} >                   By default you get 5 but those are taken up by:
}
}Default file handles are:
}
}        stdin   [0]
}        stdout  [1]
}        stderr  [2]
}        stdaux  [3]
}        stdprn  [4]
}
}It's gotta assign more than 5; if it didn't, no program could ever open
}a file!  (10, maybe?  I don't know...)

The default is FILES=8, which means 8 internal file tables, not 8 file handles--
file handles can share an internal file table if one has been DUP'ed from the
other--as stdout and stderr are by default.  The five standard file handles
normally use only three internal file tables, leaving you five additional
files to open (three if both stdin and stdout are redirected).
--
UUCP: {ucbvax,harvard}!cs.cmu.edu!ralf -=-=-=- Voice: (412) 268-3053 (school)
ARPA: ralf@cs.cmu.edu  BIT: ralf%cs.cmu.edu@CMUCCVMA  FIDO: Ralf Brown 1:129/31
			Disclaimer? I claimed something?
	You cannot achieve the impossible without attempting the absurd.

mhoffman@infocenter.UUCP (Mike Hoffman) (05/02/89)

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