[net.lang.c] VAX C help

rimbold@ulowell.UUCP (Rob Rimbold) (01/14/86)

While writing a program that does a lot of file I/O, I have come
across a few problems. I am writing integers to a file as integers using
"%d" and fprintf. I open the file using "r+". I would like to keep track
increment or simply change the integer that is already recorded in the file.
My problem is that when rewinding to the beginning of the file and
re-writing another integer the following things occur:

1. If the integer previously in the file is a single digit, only a single
	digit may be written over it, the same thing goes for double or
	triple digit integers.
	
2. If I attempt to 'reopen' the file and write into it with "w" or "w+" 
	access, and use the same file (i.e. same version all the time.),
	it won't let me. Same goes for the close-and-open ploy.

3. If I close the file, and reopen it with "r+", I still am unable to
	modify it past the number of digits that already exist there.
	I don't get errors for any of this, simply nothing happens.


Also, how does one go about deleting a file using VAX C? I was unable
to figure it out. It seems that almost every function is listed as
'not provided' in the back of the manual. I am getting desperate. 
It seems that workarounds are ever so necessary when using this cruddy
compiler. ANY ideas or suggestions would be most appreciated. Of course,
snippets of code for examples are EVEN more welcome.

'Rob


-- 
-> Rob Rimbold		"But running in parallel is more fun!"

	UUCP : 	decvax!wanginst!ulowell!rimbold
	ARPA :	rimbold@ulowell.CSNET
	AT&T :	(617) 452-5000 x2233

farren@well.UUCP (Mike Farren) (01/16/86)

In article <157@ulowell.UUCP>, rimbold@ulowell.UUCP (Rob Rimbold) writes:
> While writing a program that does a lot of file I/O, I have come
> across a few problems. I am writing integers to a file as integers using
> "%d" and fprintf. I open the file using "r+". I would like to keep track

  << He goes on to describe various problems involved with trying to rewrite
the first integer in the file >> 

    You seem to have a misconception about fprintf, somewhere.  When you
use the format '%d', fprintf will send the ascii representation of the
specified integer to the file, using only as many characters as are
necessary for that integer (i.e., 123 would use 3 characters, 65536 would
use 5).  You can get around this by specifying the field length.  For example,
with 16 bit integers (-32768 to 32767), use the format statement '%6d'.  This
will always allocate 6 characters for the representation, and this can then
be rewritten correctly.

-- 
           Mike Farren
           uucp: {your favorite backbone site}!hplabs!well!farren
           Fido: Sci-Fido, Fidonode 125/84, (415)655-0667

seaton@tron.DEC (Ian Seaton, Dedicated Mail, REO2-F/D3, DTN 830 3593) (01/17/86)

---------------------Reply to mail dated 14-JAN-1986 01:42---------------------

In his recent posting Rob Rimbold asks:
 
>While writing a program that does a lot of file I/O, I have come
>across a few problems. I am writing integers to a file as integers using
>"%d" and fprintf. I open the file using "r+". I would like to keep track
>increment or simply change the integer that is already recorded in the file.
>My problem is that when rewinding to the beginning of the file and
>re-writing another integer the following things occur:
 
>1. If the integer previously in the file is a single digit, only a single
>	digit may be written over it, the same thing goes for double or
>	triple digit integers.

	That is correct, if you open the file "r+" you can read/modify the data
	there, NOT extend it (ie 1 digit -> 3 digits). The "workaround" is to 
	write formatted data:

		 fprintf( file_desc, "%03d", number );

	or replace 3 with the maximum length of the number string. Reading is
	just as easy:

		fscanf( file_desc, "%d", &number );
	
>2. If I attempt to 'reopen' the file and write into it with "w" or "w+" 
>	access, and use the same file (i.e. same version all the time.),
>	it won't let me. Same goes for the close-and-open ploy.

	This is VAX/VMS you're talking to what did you expect?

	"w" opens a file for writing and creates a new file. If the file already
	exists, it creates a new file with the same name and a higher version
	number.

	"w+" opens a new file for write update access

>3. If I close the file, and reopen it with "r+", I still am unable to
>	modify it past the number of digits that already exist there.
>	I don't get errors for any of this, simply nothing happens.

	See 1. above.

>Also, how does one go about deleting a file using VAX C? I was unable
>to figure it out. It seems that almost every function is listed as
>'not provided' in the back of the manual. I am getting desperate.

Desperate enough to READ the manual?

	int delete( file_spec )
	char *file_spec;

	Returns 0 if successful, -1 if it fails.

>It seems that workarounds are ever so necessary when using this cruddy
>compiler. ANY ideas or suggestions would be most appreciated. Of course,
>snippets of code for examples are EVEN more welcome.

RTFM Rob...

     Share and Enjoy...

          Ian Seaton
                    DEC Reading, England

UUCP:                              !dec-thrint!
                    decwrl!dec-rhea!dec-tron  !seaton
                                   !dec-blott !

ARPA:               seaton%tron.DEC@DECWRL.ARPA