|
From: P.D. v. Z. <pd...@xs...> - 2004-01-14 20:22:52
|
Well, here's a version (didn't base it on your code, so variable names and
such are different). Note that it isn't very efficient though (it copies one
byte at a time).
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string from,to;
// Get filenames
cout << "Source file: ";
getline(cin, from);
cout << "Destination file: ";
getline(cin, to);
// Open files
ifstream is(from.c_str(), ios::binary);
ofstream os(to.c_str(), ios::binary);
if (!is.is_open() || !os.is_open())
{
cerr << "Unable to open files." << endl;
return 1;
}
// Create some iterators based on the file
unsigned char uc;
while (is)
{
uc = is.get();
if (is.eof())
break;
os.put(uc);
}
// Exit app
return 0;
}
If your application will only run on Windows (or Win32 systems in general),
you can use the "CopyFile" function defined in windows.h, which will
undoubtedly be faster.
Here's the declaration of this function, in case you're interested:
BOOL CopyFile(
LPCTSTR lpExistingFileName,
LPCTSTR lpNewFileName,
BOOL bFailIfExists
);
Hope this helped,
Paul
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Noorez
Kassam
Sent: Wednesday, January 14, 2004 7:59 PM
To: dev...@li...
Subject: [Dev-C++] File Handling
This is a code that should copy a .exe file. But i'm not sure what function
call to use to copy one bit at time. Can someone fill it in.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream out_file;
ifstream in_file;
cout << "Program copyer. The .exe extension will be added for you\n";
char name1[234];
cout << "Enter name of file: ";
cin.getline(name1, 234);
strcat(name1,".exe");
in_file.open(name1, ios::binary);
out_file.open("newfile.exe",ios::binary);
//code needed hear
system("pause");
}
_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/photos&pgmarket=en-ca&RU=http%3a%2f%2fjoi
n.msn.com%2f%3fpage%3dmisc%2fspecialoffers%26pgmarket%3den-ca
-------------------------------------------------------
This SF.net email is sponsored by: Perforce Software.
Perforce is the Fast Software Configuration Management System offering
advanced branching capabilities and atomic changes on 50+ platforms.
Free Eval! http://www.perforce.com/perforce/loadprog.html
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|