05-13 07:28
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[C/C++] Example / Reading & Writing to a Binary File 본문

Programming/C&C++

[C/C++] Example / Reading & Writing to a Binary File

cinema4dr12 2014. 6. 12. 10:56

// // main.cpp // // Created by gchoi on 2014. 5. 8.. // Copyright (c) 2014년 gchoi. All rights reserved. // #include <iostream> #include <fstream> #include <strstream> const int MAX_SIZE = 2048; using namespace std; int main(int argc, const char * argv[]) { string filename1 = "{YOUR_FILE_PATH}/img0.jpg"; string filename2 = "{YOUR_FILE_PATH}/img1.jpg"; char buff[MAX_SIZE]; ifstream ifs(filename1.c_str(), ios::in | ios::binary); if(!ifs) { cerr << "can't open input file" << endl; exit(1); } ofstream ofs(filename2.c_str(), ios::out | ios::trunc | ios::binary); if(!ofs) { cerr << "can't open output file" << endl; exit(1); } while(!ifs.eof()) { ifs.read(buff, MAX_SIZE); ofs.write(buff, ifs.gcount()); } ifs.clear((ios::eofbit | ios::failbit) ^ ifs.rdstate()); if(ifs.good() && ofs.good()) cout << "correct" << endl; else cout << "bad\n" << "state of ifs : " << ifs.rdstate() << "\nstate of ofs : " << ofs.rdstate() << endl; ifs.close(); ofs.close(); return 0; }


Comments