Write to name of a file

This is a discussion on Write to name of a file within the C++ Programming forums, part of the General Programming Boards category; Is it possible in c++ to read or write to the name of a file? Thanks in advance!...

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

    Write to name of a file

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

    Thanks in advance!
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Eh, do you mean read or write to a file, or rename a file? The answer is yes either way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    790
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    790
    Cool, thank you CornedBee!
    Compiler MSVC++ 2013 with Code::Blocks.

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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21