How to create a temporary text file in C++?

后端 未结 7 1318
北荒
北荒 2020-12-09 08:30

I\'m trying to create a temporary text file in C++ and then delete it at the end of the program. I haven\'t had much luck with Google.

Could you tell me which funct

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 09:11

    Well, assuming you have been successful in creating the temporary file, you can use the remove function to delete it.

    The function is declared in stdio.h -

    #include 
    
    int remove(const char *pathname);
    

    For example, if you want to delete a file named myfile.txt the code will be

    #include
    
    int main()
    {
      if(remove("myfile.txt") == -1)
      {
        fprintf(stderr,"Remove failed");
        exit(EXIT_FAILURE);
      }
      exit(EXIT_SUCCESS);
    }
    

    I hope by now, you already know how to create the temp file, so this should resolve your query. Hope it helps.

提交回复
热议问题