Is it possible in c++ to read or write to the name of a file?
Thanks in advance!
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.
Eh, do you mean read or write to a file, or rename a file? The answer is yes either way.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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.
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.
I was hoping to be there a function like takes the files one after the other in a folder and do something with them.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.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); }
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
Cool, thank you CornedBee!
Using Windows 10 with Code Blocks and MingW.