Is it possible to create a hidden txt file in C++? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Is it possible to create a hidden txt file in C++? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am constructing an app in Visual Studio. I need to create some files to be used in a dll, but I want the files to be hidden when viewing the folder. How can I do this in a C++ program?

Interactively, you can mark a file has hidden by right-clicking on it, selecting “Properties” and selecting “Hidden”. The question is, how can do something equivalent from a C++ program?

Answer

Use the SetFileAttributes function in the Windows API:

#include <windows.h>
#include <fstream>

std::fstream file; 
int main(){ 

   file.open("myUnhiddenFile.txt",std::ios::out); 
   file << "This is my unhidden file, that I have created just now" ; 
   file.close();

   wchar_t* fileLPCWSTR = L"myUnhiddenFile.txt"; // To avoid incompatibility
                                                 // in GetFileAttributes()
   int attr = GetFileAttributes(fileLPCWSTR);
   if ((attr & FILE_ATTRIBUTE_HIDDEN) == 0) {
       SetFileAttributes(fileLPCWSTR, attr | FILE_ATTRIBUTE_HIDDEN);
    }
   return(0);
} 
We are here to answer your question about Is it possible to create a hidden txt file in C++? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji