![](/img/trans.png)
[英]How do I retrieve file / function / line numbers from a PDB using the addresses in a client's call stack?
[英]How do I call a function to close a file using FILE *fp
在main中,我創建一個文件指針,將其傳遞給一個函數以將其打開,我想擁有一個單獨的函數來關閉文件,但是我無法關閉該文件。 我想念什么?
我沒有收到錯誤,但是我檢查的方式表明文件沒有關閉。
感謝您的幫助。
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.