1 / 8

C++ Programming: chapter 6 – iostream

C++ Programming: chapter 6 – iostream. 2014, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik. // reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line;

zaria
Télécharger la présentation

C++ Programming: chapter 6 – iostream

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik

  2. // reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt”, ); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file”; return 0; } fstream, ofstream, ifstream classes

  3. ofstream myfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary);

  4. #include <iostream> #include <fstream> using namespace std; int main () { long begin,end; ifstream myfile ("example.txt"); begin = myfile.tellg(); myfile.seekg (0, ios::end); end = myfile.tellg(); myfile.close(); cout << "size is: " << (end-begin) << " bytes.\n"; return 0; } ios::beg offset from the beginning ios::cur offset from the current ios::end offset from the end Set Position: tell and seek in C-language

  5. // reading a complete binary file #include <iostream> #include <fstream> using namespace std; ifstream::pos_type size; char * memblock; int main () { ifstream file ("example.bin", ios::in | ios::binary | ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, ios::beg); file.read(memblock, size); file.close(); cout << "the complete file content is in memory"; delete[] memblock; } else cout << "Unable to open file"; return 0; } Binary file i/o stream: read and write in C-Language

  6. #include <iostream> using namespace std; int main () { cout.setf(ios::hex, ios::basefield ); // set hex as the basefield cout << 100 << endl; cout.setf(ios::dec, ios::basefield ); // set decimal as the basefield cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout<<3.141592; cout.set return 0; } cout.width(5); cout << 12.3456 << setw(4) << 12.3456; cout << 12.3456 << setprecision(2) << 12.3456; Set flag: set flags for output stream

  7. IOS hierarchy

  8. Checking File Status

More Related