Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 658
Alexander Leon VI Avatar asked Dec 01 '22 13:12

Alexander Leon VI


1 Answers

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);
} 
like image 198
Alex Avatar answered Dec 06 '22 09:12

Alex