【发布时间】:2012-06-14 21:58:45
【问题描述】:
我正在检查一些旧的 C 代码(如下所列),以便在新项目中重新使用它,但我意识到我已经遗漏了最后的 return 语句。奇怪的是,该例程运行良好并且确实返回了正确的文件指针。谁能给我解释一下这是为什么?
FILE* openforwrite(char *name, int binary)
{
//broken out from main to keep it tidy and allow multiple output files.
FILE *file;
//first see if it exists
file = fopen(name,"r");
if (file)
{ // it does, delete it
fclose(file);
if(remove(name)) bail("Output file already exists and cannot be deleted.");
}
//now lets re-create and open it for writing
if (binary)
file = fopen(name, "wb");
else
file = fopen(name, "w");
//check it actually opened
if (!file)
bail("Error opening output file.");
//and pass the pointer back
return file; // <-- I had omitted this line initially but the routine still worked
}
【问题讨论】: