| | |
C++ copy file
![]() |
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Solved Threads: 0
I can make this program to copy diverse format wihout losts.
example:
C:\\animal.exe to D:\\animal.exe
(The File location write in locale)
This is exactly just for .txt files!
What to change to work these?
example:
C:\\animal.exe to D:\\animal.exe
(The File location write in locale)
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main () { char data[50]; char inputFile [100]; cout <<"Insert inputFilePatch :"<<endl; cin >> inputFile; ifstream inputFilePatch; inputFilePatch.open(inputFile); if(!inputFilePatch) { cout << "Wrong Location!!!\n"; system("PAUSE"); return 1; } char outputFile[100]; cout<<"Insert outputFilePatch "<<endl; cin >> outputFile; ofstream outputFilePatch; outputFilePatch.open(outputFile); outputFilePatch<<data; while(inputFilePatch>>data){ outputFilePatch<<data<< endl; }; outputFilePatch.close(); system("PAUSE"); return 0; }
This is exactly just for .txt files!
What to change to work these?
Last edited by Ancient Dragon; Nov 16th, 2007 at 6:28 pm. Reason: replaced icode tags with code tags.
If you are trying to write that code for MS-Windows then you can use win32 api CopyFile() function. Otherwise open the input and output files in binary mode, not the default text mode. On *nix it doesn't matter because they are both the same, but on MS-Windows there is a difference.
My NewYear's resolution is to lose weight.
Weight Lost since 1 Jan 2010: 8.7 lbs
Weight Lost since 1 Jan 2010: 8.7 lbs
•
•
Join Date: Dec 2006
Posts: 1,095
Reputation:
Solved Threads: 165
the easiest, least error-prone and the fastest way to copy an entire stream is this:
output_stream << input_stream.rdbuf() ;. if you are writing a utility like this, error messages are conventionally sent to cerr (not cout).
output_stream << input_stream.rdbuf() ;. if you are writing a utility like this, error messages are conventionally sent to cerr (not cout).
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std ; int main( int argc, char** argv ) { if( argc != 3 ) { cout << "usage: " << argv[0] << "<srce file> <dest file>" ; return 1 ; } ifstream srce( argv[1], ios::binary ) ; if( !srce ) { cerr << "could not open " << argv[1] << " for reading\n" ; return 2 ; } ofstream dest( argv[2], ios::binary ) ; if( !dest ) { cerr << "could not open " << argv[2] << " for writing\n" ; return 3 ; } dest << srce.rdbuf() ; if( !dest ) { cerr << "error while copying\n" ; return 4 ; } }
![]() |
Similar Threads
- how to copy a file (Python)
- Copy file with a name change (ASP)
- Cannot copy file: Data error (cylcic redundancy check) (Windows NT / 2000 / XP)
- how to copy a file from one machine to another using beans? (Java)
- copy a file (Python)
- Cannot copy file: Data error (cylcic redundancy check) (Windows NT / 2000 / XP)
- Windows XP Pro install on new machine "could not copy file .." (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: i need help......
- Next Thread: factorial
Views: 14482 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






