Just a few days ago I spent way too long looking at the boost libraries to see if there was a way of adapting a std::fstream to an iterator type. I didn't find what I was looking for and did what I wanted a completely different way.
This morning while reading Stroustrup (The C++ Programming Language) I learned that what I was looking for was already built in to the standard library!
Well, after an hour or so of work I eliminated about 40 lines of code, made my code more maintainable and more readable.
As a test case, I put together this little example. The below code is a complete program, it will compile and run and copy "input.txt" to "output.txt".
It works with any file, binary or not (which is why the noskipws is needed). No, it is NOT the most efficient way of doing things, but it shows just how expressive the C++ standard library is.
#include <istream> #include <iostream> #include <fstream> #include <iterator> using namespace std; int main() { fstream f("input.txt", fstream::in|fstream::binary); f << noskipws; istream_iterator<unsigned char> begin(f); istream_iterator<unsigned char> end; fstream f2("output.txt", fstream::out|fstream::trunc|fstream::binary); ostream_iterator<char> begin2(f2); copy(begin, end, begin2); }
Comments
thank you it worked :)I was
thank you it worked :)I was searching for this which I could not find it either.
I hope this was just an
I hope this was just an example of using iterators, since copying files can be done much simpler:
Ok - fixed that for you
#include
using namespace std;
int main()
{
ifstream f1("input.txt", fstream::binary);
ofstream f2("output.txt", fstream::trunc|fstream::binary);
f2 << f1.rdbuf(); <--- not f1 << f2.rdbuf()
}
computer systems & networks
it does compile but i havent got expected result which is its should create new file :
Not sure why this wouldn't
Not sure why this wouldn't work, unless there is an issue for how files are created on windows machines. I'm not as familiar with the details of C++ file streams on windows, if there even is a difference.
On a side note, you could use the
copy_filefunction ofboost::filesystem.copy_filedoes an OS call to copy the file which is more than likely orders of magnitude faster than anything we might discuss here. Also, it is the precursor to the filesystem access code which will be available in the next rev of C++ and is portable across many platforms. Something to consider if you are able to use boost in your project.x << y.rdbuf() doesn't always do what you want
While that's OK for small files, if the input file is larger than the data in the buffer, it doesn't work.
Correction
Actually it works fine, when you use a compiler from this century :|
I was wondering about that
Thank you for commenting on this. I had wondered about that solution but had not taken the time to research it myself.
This just solidifies my recent opinion that boost::filesystem::copy_file, which is set to become part of the next C++ standard is the best and most portable way to copy a file in C++.
Copying
How can I copy more files at once?
I have list of files, that have to be copied, in ostatakaudio.txt on desktop.
Like this:
"M:\Users\Josip\Music\Klasično\Radetzkijev marš-J. Strauss stariji.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\Za Elizu-L. van Beethoven.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\Carlos Santana-Europa.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\Apache.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\White Snake - Here I Go Again .mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\Theme from Forrest Gump.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\Chuck Berry - Johnny B. Goode.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\100 Piano Classics Disc 4\06 Shcubert - Musical Moment No 3.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\J. Strauss st.-Glasovi proljeća.mp3" "M:\Users\Josip\Desktop\glazba"
I'll explain. First ist source file ("M:\Users\Josip\Music\Klasično\Radetzkijev marš-J. Strauss stariji.mp3") then space and then destination for copying ("M:\Users\Josip\Desktop\glazba") in my case folder glazba.
My precise question: How to read one line at the time and, like that, copy all of the listed files in ostatakaudio.txt??
How to add that to the above code?
Please answer.
COPYING HELP!!!!!!!!!! PLEASE!!!!!!!!!!!
How can I copy more files at once?
I have list of files, that have to be copied, in ostatakaudio.txt on desktop.
Like this:
"M:\Users\Josip\Music\Klasično\Radetzkijev marš-J. Strauss stariji.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\Za Elizu-L. van Beethoven.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\Carlos Santana-Europa.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\Apache.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\White Snake - Here I Go Again .mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\Theme from Forrest Gump.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\SVAŠTA!!!!!!!!!!!!!!!!!\Chuck Berry - Johnny B. Goode.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\100 Piano Classics Disc 4\06 Shcubert - Musical Moment No 3.mp3" "M:\Users\Josip\Desktop\glazba"
"M:\Users\Josip\Music\Klasično\J. Strauss st.-Glasovi proljeća.mp3" "M:\Users\Josip\Desktop\glazba"
I'll explain. First ist source file ("M:\Users\Josip\Music\Klasično\Radetzkijev marš-J. Strauss stariji.mp3") then space and then destination for copying ("M:\Users\Josip\Desktop\glazba") in my case folder glazba.
My precise question: How to read one line at the time and, like that, copy all of the listed files in ostatakaudio.txt??
How to add that to the above code?
Please answer.