alexa-tracking

Main Content

1024
1024
KASKUS
51
244
https://www.kaskus.co.id/thread/52e211d2bdcb1754038b463f/tanya-c-copy-file
[tanya C++] copy file
code 1
Code:
 #include <fstream>
int main()
{
std::ifstream src("coba.txt", std::ios::binary);
std::ofstream dst("coba_baru.txt", std::ios::binary);

dst << src.rdbuf();

return 0;
}

code2
Code:
#include <fstream>
#include <iostream>
int main()
{
std::string inFile;
std::string outFile;

inFile = "coba.txt";
outFile = inFile.insert(inFile.length()-4,"_baru");

std::ifstream inBuff((inFile.c_str()), std::ios::binary);
std::ofstream outBuff((outFile.c_str()), std::ios::binary);
outBuff << inBuff.rdbuf();

return 0;
}
Halo selamat siang,
tanya nih gan, itu code buat copy file,
* pakai code1 hasilnya OK.
* pakai code2 hasilnya: file copy-annya, coba_baru.txt, muncul tapi isinya kosong, ada yang tau kenapa ya??
buat penjelasannya terima kasih banyak-
coba tambahin yg ane warnain merah:

Spoiler for :
ga bisa om, itu cuma nampilin alamat pointernya di layar
Quote:Original Posted By cak9empol
ga bisa om, itu cuma nampilin alamat pointernya di layar


sotoy lu ah..
cobain aja dulu, liat apa yg ditampilin di layar..

http://ideone.com/ba7a1V < liat output(stdout)-nya..
haha sori bang, ane kira suruh nampilin fstream-nya emoticon-Big Grin
ternyata itu fungsi insert ngubah string asalnya juga ya
asem malu gw emoticon-Frown((
sekarang sudah bisa pake code dibawah,
TQ bang sarannya emoticon-Wink
Code:
#include <fstream>
#include <iostream>

int main()
{
std::string inFile, outFile, temp;

inFile = "coba.txt";
temp = inFile;
outFile = temp.insert(inFile.length()-4,"_baru");

std::cout << "file input: " << inFile << std::endl;

std::cout << "file output: " << outFile << std::endl;

std::ifstream inBuff((inFile.c_str()), std::ios::binary);
std::ofstream outBuff((outFile.c_str()), std::ios::binary);
outBuff << inBuff.rdbuf();

return 0;
}