简体   繁体   English

如何使用FILE * FP调用函数来关闭文件

[英]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.

相关问题 如何使用客户端调用堆栈中的地址从PDB检索文件/函数/行号? - How do I retrieve file / function / line numbers from a PDB using the addresses in a client's call stack? 如何将文件描述符(int fd)转换为文件指针(FILE * fp) - How can I convert a file descriptor (int fd) to a file pointer ( FILE* fp ) 如果我到达文件C ++的结尾,如何关闭文件 - How do i close file if I reach end of file c++ 复制文件时如何调用取消? (将复制文件ex函数与复制进度例程回调函数一起使用) - How can I call cancel while copying file? (using copy file ex function with copy progress routine callback function) 如何在C ++中的另一个mex(MATLAB)目标文件中调用一个cuda目标文件的函数? - How do I call one cuda object file's function inside other mex (MATLAB) object file in c++? 如何使用着色器从文件中的类调用以在包含gl代码的另一个文件中显示(两个文件都位于jni文件夹中)? - how do I call from a class that is in a file to display in another file containing the gl code using shaders (where both files are in jni folder)? 如何调用此功能? - How do I call this function? (C ++)如何从另一个文件调用结构? - (C++) How do I call on a structure from another file? 如何使用dlopen()和dlsym()在.so文件中调用函数? - How to call function in .so file using dlopen() and dlsym()? 如何使用php脚本从.so文件调用函数? - How to call function from .so file using php script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM