1820614 Members
1827 Online
109626 Solutions
New Discussion юеВ

Re: File Pointers

 
SOLVED
Go to solution
ram_47
Frequent Advisor

File Pointers

Hi,

1) I declare 2 file pointers -
FILE *fp1, *fp2;

2) Using 'fp1' I open a file for reading in binary mode -
fp1=fopen("test","rb");

3) I use 'fp2' to point to same memory what fp1 is pointing to -
fp2=fp1;

4) Using 'fp2' I read a record from the file 'test' using a structure -
fread(&rec,sizeof(struct st),1,fp2);


Question -
What is the impact on pointer 'fp1' after step #4 is executed? Where will 'fp1' point to?


My Observation -
After step #4 is executed, 'fp2' should be at the end of 1st record that is read. and since 'fp1' is never used it should still be pointing to the beginning of the file. but it does not work that way. what i find is though 'fp1' is not used, it too is at the end of the 1st record along with 'fp2'.

i want 'fp1' to remain at the beginning of the file untill i position it. is this something that i could achieve? pl help.
23 REPLIES 23
Kris Clippeleyr
Honored Contributor

Re: File Pointers

Hi Ram,

Since something of the type FILE * is a pointer to something (in this case an object of the opaque type FILE), it is only natural that if an operation changes something in that object, both pointers to the same object reflect those changes.

You might want to read on the functions fseek() and ftell(); these can be used to (re)position the read/write marker in a file.

Regards,
Kris (aka Qkcl)
I'm gonna hit the highway like a battering ram on a silver-black phantom bike...
Antoniov.
Honored Contributor
Solution

Re: File Pointers

Hi Ram,
if you want to keep two different file pointer on the same file you have to open file two times.

FILE *fp1, *fp2;
fp1=fopen("test","rb");
fp2=fopen("test","rb");

Antonio Vigliotti


Antonio Maria Vigliotti
John Gillings
Honored Contributor

Re: File Pointers

fp1 and fp2 are pointers to the same data structure. Just as multiple pointers to the same variable will all "see" the same value, regardless of which pointer was used to set the value, both file pointers will always "see" the same view of the file.

Opening the file twice will do what you want. HOWEVER, you need to open the file with shared access. The two opens from the same process are seen in the same way as opens from multiple processes - hence shared access is required.

If you used RMS I/O you could OPEN the file once and CONNECT multiple streams. This would allow you to maintain multiple pointers into the file, but with exclusive access. Native C I/O doesn't have a way to access multiple RMS streams (BASIC and COBOL do).

A crucible of informative mistakes
ram_47
Frequent Advisor

Re: File Pointers

Am not quite familiar with RMS. Where could I find documentation on RMS, if possible with examples.
David B Sneddon
Honored Contributor

Re: File Pointers

ram,

There are a number of examples in SYS$EXAMPLES showing various
features of RMS.

The documentation can be found by following the link at the
bottom of the main page for this forum.

http://h71000.www7.hp.com/doc/index.html

Dave
David B Sneddon
Honored Contributor

Re: File Pointers

ram,

The RMS reference manual is at

http://h71000.www7.hp.com/doc/731FINAL/4523/4523PRO.HTML

which covers all the services and has various
examples, some in C.

Dave
Antoniov.
Honored Contributor

Re: File Pointers

Hi
RMS programming is not simple and (in this case) can't help.
Neither fopen(), neither fread() of standard c-runtime doesn't lock record.
Using of RMS is useful to manage indexed files, not binary/stream flushes.
Full working example is attached.

Antonio Vigliotti
Antonio Maria Vigliotti
ram_47
Frequent Advisor

Re: File Pointers

How can I have a look at SYS$EXAMPLES. Am sorry but I am quite new to VMS.
David B Sneddon
Honored Contributor

Re: File Pointers

$ dir sys$examples:*.c

will list all the C programs.

$ type/page sys$examples:alpha_logger.c

will list alpha_logger.c

You may find it easier to look at the examples
in the documentation.

Dave
Antoniov.
Honored Contributor

Re: File Pointers

Hi RAM,
SYS$EXAMPLES is a logical name; you can think to this like a mapped disk of windows. You can simply type
DIR SYS$EXAMPLES:*.C
However, I don't think you can solve your dubt.
Would you see some RMS source code?

Antonio Vigliotti
Antonio Maria Vigliotti
ram_47
Frequent Advisor

Re: File Pointers

The binary files that am talking about are Indexed RMS VMS binary file.
ram_47
Frequent Advisor

Re: File Pointers

Using fsetpos and fgetpos am able to get what I wanted.
ram_47
Frequent Advisor

Re: File Pointers

When I used those above functions and read records from the file, each time I get an extra record, which is obviously a junk record.

are there any special considerations while using those functions?
Antoniov.
Honored Contributor

Re: File Pointers

Hi Ram,
attached file is source for RMS files manager I use for some years.
Sorry, remarks are in Italian language, because I use it for personal purpose.
Function are like stdio file function. For example, file pointer is declared by
XFILE *fp;
---------------------------------
Function are:

fp = xopen(filename, mode);
open a RMS file; mode may be r,w,b,a that means Read only, Write only, Both (read & write), Append.
File pointer is null RMS file can't be opened.

xclose(fp);
close a RMS file.

buf_prt = xread (buf_ptr, key_str_ptr, index, max_bytes, fp);
read a record from file opened by xopen with exact key. Main index is 0. If function can't read, returns null.

buf_prt = xseek (buf_ptr, key_str_ptr, index, max_bytes, fp);
read a record from file opened by xopen with key greater of equal. Main index is 0. If function can't read, returns null.

buf_prt = xnext (buf_ptr, fp);
read a next record after xread o xseek.If function can't read, returns null.

bytes = xwrite (buf_ptr, max_bytes, fp);
write a new record into file opened by xopen.

bytes = xrewrite (buf_ptr, max_bytes, fp);
rewrite a record after xread.

BOOL = xdelete (fp);
delete current record after xread.

Antonio Vigliotti



Antonio Maria Vigliotti
Antoniov.
Honored Contributor

Re: File Pointers

Hi,
here there is xfile.h to compile previous file.
I forgot some other functions:

settmo (fp, timeout);
set time-out when read locked record.

setlmode (fp, mode);
set lock mode; mode may be r for Read (record is not locked by xread o xseek) or w for Write (record is locked).

errc = xerror (fp);
return last error code.

Antonio Vigliotti
Antonio Maria Vigliotti
Ian Miller.
Honored Contributor

Re: File Pointers

Antonio, parhaps you could make your library of routines available as freeware on Hunter Goatley's fileserv site and/or the next VMS Freeware collection. All software greatfully accepted - they even take some of mine :-)
____________________
Purely Personal Opinion
Antoniov.
Honored Contributor

Re: File Pointers

Ian,
how can I made it?
Have I translate remarks?

Antonio Vigliotti
Antonio Maria Vigliotti
ram_47
Frequent Advisor

Re: File Pointers

Hi Antonio,

Thanks for the source. I need to look at those files at leisure as they seem to be complex :)
Ian Miller.
Honored Contributor

Re: File Pointers

Anonito, go to
http://www.process.com/openvms/index.html
for more info on fileserv. email Hunter Goatley (address on that web page) as he is very helpful and will explain packaging requirements.

For VMS freeware go to
http://h71000.www7.hp.com/openvms/freeware/cd_guide.html

____________________
Purely Personal Opinion
Hein van den Heuvel
Honored Contributor

Re: File Pointers

Antionio,

>> pxRcb->rab$l_rop = (RAB$M_NLK|RAB$M_RRL); /* Leggi anche locked */

Be sure to read up on th new ( 5 years ? :-) RMS RAB$M_NQL option. This will truly not do any record lock activity as you probably intent. The NLK+RRL will not keep a lock, and read regardless of any existing record lock, but it will take out and release a record lock. This overhead should often be avoided.

Hein.

Antoniov.
Honored Contributor

Re: File Pointers

Hein,
happy to answer you.
The statement you posted can read a RMS record regardless of lock.
It the same of DCL read after open/share=write.
When I want to be sure record is free, I use
xsetlmode(fp,"r") function in the same library I attached.

Antonio Vigliotti

Antonio Maria Vigliotti
Hein van den Heuvel
Honored Contributor

Re: File Pointers

[ram, sorry for hijacking your threat]

>> The statement you posted can read a RMS record regardless of lock.

Of course.

In my earlier reply I just wanted to point out that, contrary to popular belief, this line will actually cause a VMS lock to be taken and released.

If you use that procedure to read a file with millions of records from begin to end then this will be a significant system overhead.

You can avoid this 'overhead' by also using the newish NQL option.

hth,
Hein.
Antoniov.
Honored Contributor

Re: File Pointers

Ram,
I also have to say
"sorry for hijacking your thread"

Hein,
interesting,
today I try then I report you the result.
Thank you.

Antonio Vigliotti
Antonio Maria Vigliotti