title: How to hide/un-hide a file without erasing other attributes in C++ on Windows
body: <p>I would like to be able to hide/un-hide a file in Windows in C++, but I was concerned about erasing other attributes (like FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE, ...).</p>
  
  <p>Here is the current code:</p>
  
  <pre><code>//Hiding the file
  SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN);
  
  // Un-Hiding the file
  SetFileAttributes(path, FILE_ATTRIBUTE_NORMAL);
  </code></pre>
  
  <p>This works fine for regular files, but will hiding the file remove a READONLY flag for example? Will unhiding the file remove it?</p>
  
  <p>If yes, I was planning on doing something like this:</p>
  
  <pre><code>//Hiding the file
  int attr = GetFileAttributes(path);
  if ((attr | FILE_ATTRIBUTE_HIDDEN) == 0) {
      SetFileAttributes(path, attr &amp; FILE_ATTRIBUTE_HIDDEN);
  }
  
  //Unhiding the file
  int attr = GetFileAttributes(path);
  if ((attr | FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN) {
      SetFileAttributes(path, attr &amp; FILE_ATTRIBUTE_HIDDEN);
  }
  </code></pre>
  
  <p>Would that work?</p>