#include <stdio.h>
FILE * Openfile(char *filename,char *mode, FILE *fp);
int main(){
FILE *fp=NULL;
char *filename = "simple_index.file";
char *openmode = "w";
printf("FP(before call function):%p
", fp);
FILE *newfp = Openfile(filename, openmode, fp);
printf("FP(after call function): %p
NEWFP: %p
", fp, newfp);
return 0;
}
FILE * Openfile(char *filename,char *mode, FILE *fp){
printf((fp = fopen(filename, mode)) ? "Good opening %s file
": "Error open %s file
", filename);
return fp;
}
Result:
FP(before call function):0x0
Good opening simple_index.file file
FP(after call function): 0x0
NEWFP: 0x800bc7140
A pointer to the file structure does not store the address of an open file after using a function to open a file in the call Openfile()
function.
Why does FP
not save the state after use in function? Why need back for save?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…