为什么文件指针的行为不同于C语言中的普通指针?
Why file pointer acts different from normal pointer in C language?
这是我的 C 代码:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"limits.h"
int main(int argc,char **argv){
int Array[2]={100,150};
int *pArray=Array;
int *qArray=pArray;
qArray++;
printf("pArray address:%p\n",pArray);
printf("qArray address:%p\n",qArray);
if(pArray!=qArray){
printf("pArray not equals to qArray\n\n");
}
FILE *fp1=NULL,
*fp2=NULL,
*fp3=NULL,
*fp4=NULL,
*fp5=NULL;
fp1=fopen("test.bin","rb+");
if(fp1==NULL){
return 1;
}
fp2=fp1;
fp3=fp1;
fp4=fp1;
fp5=fp1;
fseek(fp2,100l,SEEK_CUR);
fseek(fp3,200l,SEEK_CUR);
fseek(fp4,300l,SEEK_CUR);
fseek(fp5,400l,SEEK_CUR);
printf("fp1 position:%lu\n",ftell(fp1));
printf("fp2 position:%lu\n",ftell(fp2));
printf("fp3 position:%lu\n",ftell(fp3));
printf("fp4 position:%lu\n",ftell(fp4));
printf("fp5 position:%lu\n",ftell(fp5));
if(fp1==fp5){
printf("fp1 equals to fp5.\n");
}
fclose(fp1);
return 0;
}
这是输出:
$ ./program
pArray address:0x7ffe5e6780a0
qArray address:0x7ffe5e6780a4
pArray not equals to qArray
fp1 position:1000
fp2 position:1000
fp3 position:1000
fp4 position:1000
fp5 position:1000
fp1 equals to fp5.
我得出一个结论:移动普通指针不会影响与其共享地址的指针,但是移动具有fseek
功能的文件指针会影响。
为什么文件指针的行为与普通指针不同?
A FILE *
不是指向文件的指针;它是指向 FILE
的指针。根据 C 2018 7.21.1 2,FILE
是:
… an object type capable of recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached…
因此fp2=fp1;
使fp2
指向用于控制文件的同一对象,因此对fp1
指向的对象所做的任何更改都会出现在指向的对象中到fp2
,反之亦然,因为它们是同一个对象。
qArray++
更改指针 qArray
。 fseek(fp2,100l,SEEK_CUR);
更改 fp2
指向的对象(及其控制的流)。同样,如果您调用某个例程 ChangeAnElementInArray(qArray, index, value)
,那将更改数组中的一个元素;它不会改变 qArray
指针。
此外,FILE *
不是文件中的位置。它只是指向用于控制流的数据结构的指针。
这是我的 C 代码:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"limits.h"
int main(int argc,char **argv){
int Array[2]={100,150};
int *pArray=Array;
int *qArray=pArray;
qArray++;
printf("pArray address:%p\n",pArray);
printf("qArray address:%p\n",qArray);
if(pArray!=qArray){
printf("pArray not equals to qArray\n\n");
}
FILE *fp1=NULL,
*fp2=NULL,
*fp3=NULL,
*fp4=NULL,
*fp5=NULL;
fp1=fopen("test.bin","rb+");
if(fp1==NULL){
return 1;
}
fp2=fp1;
fp3=fp1;
fp4=fp1;
fp5=fp1;
fseek(fp2,100l,SEEK_CUR);
fseek(fp3,200l,SEEK_CUR);
fseek(fp4,300l,SEEK_CUR);
fseek(fp5,400l,SEEK_CUR);
printf("fp1 position:%lu\n",ftell(fp1));
printf("fp2 position:%lu\n",ftell(fp2));
printf("fp3 position:%lu\n",ftell(fp3));
printf("fp4 position:%lu\n",ftell(fp4));
printf("fp5 position:%lu\n",ftell(fp5));
if(fp1==fp5){
printf("fp1 equals to fp5.\n");
}
fclose(fp1);
return 0;
}
这是输出:
$ ./program
pArray address:0x7ffe5e6780a0
qArray address:0x7ffe5e6780a4
pArray not equals to qArray
fp1 position:1000
fp2 position:1000
fp3 position:1000
fp4 position:1000
fp5 position:1000
fp1 equals to fp5.
我得出一个结论:移动普通指针不会影响与其共享地址的指针,但是移动具有fseek
功能的文件指针会影响。
为什么文件指针的行为与普通指针不同?
A FILE *
不是指向文件的指针;它是指向 FILE
的指针。根据 C 2018 7.21.1 2,FILE
是:
… an object type capable of recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached…
因此fp2=fp1;
使fp2
指向用于控制文件的同一对象,因此对fp1
指向的对象所做的任何更改都会出现在指向的对象中到fp2
,反之亦然,因为它们是同一个对象。
qArray++
更改指针 qArray
。 fseek(fp2,100l,SEEK_CUR);
更改 fp2
指向的对象(及其控制的流)。同样,如果您调用某个例程 ChangeAnElementInArray(qArray, index, value)
,那将更改数组中的一个元素;它不会改变 qArray
指针。
此外,FILE *
不是文件中的位置。它只是指向用于控制流的数据结构的指针。