1752806 Members
5985 Online
108789 Solutions
New Discussion юеВ

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