| | |
C++ copy file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






