Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File pointers

Status
Not open for further replies.

RamHardikar

Programmer
Feb 16, 2001
109
GB
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?
 
As fp1 and fp2 are both simply addresses in memory which point to FILE structures then
Code:
fread(&rec,sizeof(struct st),1,fp1);
Code:
fread(&rec,sizeof(struct st),1,fp2);
are absolutely equivalent. The FILE structure pointed to by both pointers will have changed but not the pointers themselves.

Columb Healy
 
I asked an almost identical question in the tek-tips pascal forum. The general opinion seemed to be that this (allocating a file to two file structure variables) was unnecessary and probably undesirable; how it behaves might even vary with how the operating system handles files (for instance file-control-block or file-handle approaches may vary in how they behave)

Of course, that was Pascal, this is C, but they are similar languages with similar underlying structure, operating in the same operating system environment, and addressing the same problems. Turbo Pascal actually forbids assigning one file to another. You can get round it, and it works, but anything the writers of TP felt worth preventing, I'd be dubious of using.
 
But we're not allocating two file structre variables, we're allocating one and then pointing two pointers at it. As such it doesnt matter which pointer you use, they both point to the same structure in memory. To pass one in a function is identical to passing the other.

Columb Healy
 
OK, step by step

1) Pointers declared

2) FILE structure allocated at 0xABCDEF
fp1 = 0xABCDEF

3) fp2 = 0xABCDEF

4) Data read using FILE structure at 0xABCDEF
(fp1 and fp2 unchanged with value at 0xABCDEF)


Cheers,

Dian


 
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.
 
Also, is there a way to reposition file pointer backwards? By backwards i mean 'n' bytes back and not to the beginning of file.
 
how cud u move backwards using fseek? i tried but it doesnt work. appreciate if u cud give me an example
 
Hi,
what is your operating system?
I am not quite sure whether I ever did it, but it should work on Unix; this is from my manual pages:
man fseek said:
int fseek(
FILE *stream,
long int offset,
int whence );
...
The fseek() function sets the position of the next input or output opera-
tion on the I/O stream specified by the stream parameter. The position of
the next operation is determined by the offset parameter, which can be
either positive or negative.
 
i tried giving (-)ve offset but it did not work. my OS is VAX VMS.
 
hm ....,
I never did C programming on VAX VMS; anybody out there?
What do your VMS manual pages say about fseek and offsets?
If negative offsets are impossible, a workaround could be to use a positive offset, not from your current position, but from the beginning of the file.
ftell() might be useful, but I don't know if this exists on VMS.
hope this helps
 
I've programmed C on VAX VMS, but it's been a VERY long time! As I recall, the [tt]fseek()[/tt] and [tt]ftell()[/tt] functions work as expected. If you want to seek to a negative offset, but it's not working, maybe try something like this...
Code:
#include <stdio.h>

long   offset = -100;

fseek( fp1, ftell(fp1) + offset, SEEK_SET );

If you want [tt]fp1[/tt] and [tt]fp2[/tt] to work independently, you need to open the file twice...
Code:
FILE *fp1, *fp2;

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

fread(&rec,sizeof(struct st),1,fp2); 

// fp1 is still at beginning of file, fp2 has moved
Just copying a pointer to the FILE structure won't do it, because it's that single FILE structure that maintains the position into the file.

Hope this helps.
 
instead of using two file pointers you
might use "fpos_t filepos" to allow you
to store a file position. kerny and richey
state that this type is suitable for recording
such values.

Code:
#include <stdlib.h>
#include <stdio.h>

void showpos(FILE *stream);

int main(void)
{
   FILE *stream;
   fpos_t filepos;

   /* open a file for update */
   stream = fopen("DUMMY.FIL", "w+");

   /* save the file pointer position */
   fgetpos(stream, &filepos);

   /* write some data to the file */
   fprintf(stream, "This is a test");

   /* show the current file position */
   showpos(stream);

   /* set a new file position, display it */
   if (fsetpos(stream, &filepos) == 0)

     showpos(stream);
   else
   {
      fprintf(stderr, "Error setting file pointer.\n");
      exit(1);
   }

   /* close the file */
   fclose(stream);
   return 0;
}

void showpos(FILE *stream)
{
   fpos_t pos;

   /* display the current file pointer 
      position of a stream */
   fgetpos(stream, &pos);
   printf("File position: %ld\n", pos);
}

TC
 
Thanks to SamBones and Butthead. Your solutions work!
For the moment I am using fgetpos and fsetpos.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top