[英]How do I call a function to close a file using FILE *fp
In main I create a file pointer, pass that to a function to open it and I want to have a separate function to close the file but I am not able to close the file. 在main中,我创建一个文件指针,将其传递给一个函数以将其打开,我想拥有一个单独的函数来关闭文件,但是我无法关闭该文件。 What am I missing? 我想念什么?
I do not get an error but the way that I am checking indicates the files did not close. 我没有收到错误,但是我检查的方式表明文件没有关闭。
thanks for help on this. 感谢您的帮助。
int _tmain(int argc, _TCHAR* argv[])
{
FILE *inFile, *outFile;
FileOpen(&inFile, &outFile);
FileClose(&inFile, &outFile);
getch();
return 0;
}
void FileOpen(FILE **inFile, FILE **outFile)
{
//--Open InFile--------------------------------
if ((*inFile = fopen("a.txt","r")) == NULL){
printf("\nError Opening File. ");
exit(0);
}
else {
printf("File Opened\n");
}
//--Open OutFile--------------------------------
if ((*outFile = fopen("b.txt","w")) == NULL){
printf("\nError Opening File. ");
exit(0);
}
else {
printf("File Opened\n");
}
}
void FileClose(FILE **inFile, FILE **outFile)
{
//--Close InFile----------
if (fclose(*inFile)){
printf("File Closed\n");
}
else {
printf("\nError Closing InFile File. ");
}
//--Close outFile---------
if (fclose(*outFile)){
printf("File Closed\n");
}
else {
printf("\nError Closing OutFile File. ");
}
}
您误解了返回值 : fclose
成功返回0,因此您需要检查if (fclose(*inFile) == 0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.