setting file attribute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan12345
    New Member
    • Apr 2007
    • 2

    setting file attribute

    Hi there. Im a new member here in the forum.

    I've got a question to ask.

    How do I set the file attribute of a file using C++ coding?
    Is there any function when called that will return the state of the file, as in 'hidden', 'archive' and all the file attributes?
  • Bruce Mac
    New Member
    • Mar 2007
    • 4

    #2
    hi, not sure if this will help but it tried it and it worked for me.

    cout<< system("attrib c:/test.txt") << endl;

    assuming that you have a file called test.txt in the root of your c drive.
    also to modify the attributes open a dos window and type 'attrib /?' with out quotes and it will give you the switches to modify the attributes.

    Bruce

    Comment

    • Dan12345
      New Member
      • Apr 2007
      • 2

      #3
      Hi there. What I meant was to set the attribute of a file using C++ coding. In other words, I would like to create a file using C++ coding and then set the file attribute to be 'hidden' (for example) so that when I go into the directory, i do not see hidden files.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This has nothing to do with C++.

        This is an operating system setting. Research your system calls.

        Comment

        • Nierburt
          New Member
          • Dec 2007
          • 1

          #5
          Yes, C++ can do the trick. If you were paying attention to what Bruce Mac said, typing “attrib /?” without the quotes on a dos window will give you the possible switches for modifying the file attributes. Thus, after creating an output file (e.g)
          ofstream out_stream("tes t.txt");
          Follow it with this line:
          system("attrib +h test.txt"); //Set the hidden attribute for test.txt
          Or if you want, you can also add the “system” attribute:
          system("attrib +h +s test.txt");

          Note: test.txt is on the same folder with the executable. You can replace it with the absolute path of the file you are concerned with.

          I hope this works for you.

          Comment

          Working...