Thread: Write to name of a file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Write to name of a file

    Is it possible in c++ to read or write to the name of a file?

    Thanks in advance!
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, do you mean read or write to a file, or rename a file? The answer is yes either way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Laserlight!

    Actually what i wanted to do is open every file in a folder and rename them.

    Like numbering them for example :
    1_filename.txt
    2_filename.txt
    3_filename.txt

    But without specifying their names i dont think that its possible to open them, is it?
    Last edited by Ducky; 04-13-2008 at 07:41 AM.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You don't need to open files in order to rename them. In fact, it would be quite contra-productive, as some OSs (like Windows) disallow renaming of files that are open.

    But you do need the original name to rename files, yes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can use rename to do the renaming.

    First you'll need a list of the files in that folder. On Windows, I believe this might be done with FindFirstFile and FindNextFile.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I was hoping to be there a function like takes the files one after the other in a folder and do something with them.
    Actually its not even a bad idea, maybe they should think about on writing one.

    Thank you though.
    Last edited by Ducky; 04-13-2008 at 08:19 AM.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Well that 'rename' function you mention Anon still need to know the name of the file, it can be
    useful in some cases but not for what i want to do.
    But these FindFirstFile and FindNextFile sounds more like what i need.

    Thanks for mentioning.
    Last edited by Ducky; 04-13-2008 at 08:18 AM.
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I was hoping to be there a function like takes the files one after the other in a folder and do something with them.
    Code:
    #include <algorithm>
    #include <boost/filesystem.hpp>
    namespace fs = boost::filesystem;
    
    void do_something(const fs::path &p)
    {
      // Go on and do something. (But don't rename here - that would lead to chaos.)
    }
    
    int main()
    {
      fs::path dir = fs::initial_path();
      std::for_each(fs::directory_iterator(dir), fs::directory_iterator(), do_something);
    }
    There's a reason why you shouldn't rename, though: doing so modifies the contents of the directory you're iterating over, and thus modifies the sequence you're iterating over. So instead, you should first collect the file names and then iterate over that collection and do the renames.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Cool, thank you CornedBee!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM