以下两种方法中的一种比另一种有优势吗?
这里首先测试它是否fopen
成功,然后发生所有变量声明,以确保它们没有被执行,因为它们必须不必
void func(void) {
FILE *fd;
if ((fd = fopen("blafoo", "+r")) == NULL ) {
fprintf(stderr, "fopen() failed\n");
exit(EXIT_FAILURE);
}
int a, b, c;
float d, e, f;
/* variable declarations */
/* remaining code */
}
这恰恰相反。所有变量声明都会发生,即使fopen
失败
void func(void) {
FILE *fd;
int a, b, c;
float d, e, f;
/* variable declarations */
if ((fd = fopen("blafoo", "+r")) == NULL ) {
fprintf(stderr, "fopen() failed\n");
exit(EXIT_FAILURE);
}
/* remaining code */
}
第二种方法是否会产生任何额外的成本,当fopen
失败时?很想听听你的想法!