1 / 22

C++ I/O and Other Topics

C++ I/O and Other Topics. Using C++ Stream I/O. Default input stream is called cin Default output stream is called cout Use the extraction operator >> with cin Use the insertion operator << with cout We will see later that cin and cout are actually objects. Using cin.

taite
Download Presentation

C++ I/O and Other Topics

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++ I/O and Other Topics

  2. Using C++ Stream I/O • Default input stream is called cin • Default output stream is called cout • Use the extraction operator >> with cin • Use the insertion operator << with cout • We will see later that cin and cout are actually objects

  3. Using cin • integers and floats • cin >> i1 >> f1; // skips over whitespace • characters • cin >> ch1 >> ch2; // skips over whitespace • To not skip whitespace, use cin.get: • ch = cin.get(); // reads any character

  4. Using cin (con’t) • strings (char*) • cin >> str; // reads until first whitespace • Better to use cin.getline: • cin.getline(str, buffer_size, end_char); • where buffer_size = size of str, including ‘\0’ • end_char = character to stop at • Reading stops when end_char is encountered, the end-of-file is encountered, or when buffer_size-1 has been exceeded. ‘\n’ is the default for end_char. Automatically appends ‘\n’ to the string.

  5. Using cout • integers and floats • cout << i1 << f1; // displayed in default format • characters • cout << ch; // single character displayed • strings (char*) • cout << str; // displays up to ‘\0’ character

  6. General File I/O Steps • Declare a file name variable • Associate the file name variable with the disk file name • Open the file • Use the file • Close the file

  7. C++ File I/O • Declare a file name variable • #include <fstream.h> • ifstream input_filename_var; // input file • ofstream output_filename_var; // output file

  8. C++ File I/O (con’t) • Associate the file name variable with the disk file name and open it • input_filename_var.open(“pathname/filename”, ios::in); • output_filename_var.open(“pathname/filename”, ios::out); • where ios::in and ios::out are optional • Files may be opened in other modes such as ios::app (append) and ios::binary (binary input or output)

  9. C++ File I/O (con’t) File name declaration and opening/association may be combined: ifstream input_filename_var(pathname/filename, ios::in); ofstream input_filename_var(pathname/filename, ios::out); where ios::in and ios::out are optional

  10. C++ File I/O (con’t) • Use the file • Use an input file as you would use the cin input stream • ifile1 >> x >> y; // x and y are integers • ifile2 >> ch; // ch is a char • ch = ifile3.get(); // ch is a char • ifile4.getline(buffer, buffer_size) // buffer is char*

  11. C++ File I/O (con’t) • Use an output file as you would use the cout output stream • ofile1 << x << y; // x and y are integers • ofile2 << ch; // ch is a char • ofile3 << “Hi there!” << endl; // literal string • ofile4 << str; // str is a char*

  12. C++ File I/O (con’t) • Close the file • input_filename_var.close(); • output_filename_var.close(); • All files are closed automatically upon termination of program execution, but it is a good habit to close them explicitly. Also, close them as soon as they are no longer needed by the program.

  13. Checking That Files Have Opened Successfully • Always check that all files (input or output) have been successfully opened • If any file cannot be opened, send a message to the user and terminate program execution • ifstream myFile(“inputData”); • if (!myFile) { • cerr << “Input file could not be opened” << endl; • exit(1); // must #include <stdlib.h> to use exit function • }

  14. Checking for End-of-File ifstream inFile(“somefile.txt”); . . . if (inFile) . . . OR while (inFile) . . . Checks to see if the last operation on inFile was successful. If yes, then the condition is true; otherwise, it is false. So, if the last operation was a read, then you are checking to see if the end-of-file has been reached.

  15. Sample Program Using File I/O #include <fstream.h> #include <iostream.h> #include <stdlib.h> void main() { ifstream inFile(“numbers.dat”); int x; if (!inFile) { cerr << “Cannot open input file. Exiting.” << endl; exit(1); }

  16. Sample Program (con’t) inFile >> x; while (inFile) { // OR while (inFile >> x) and inFile >> x; // omit the priming read cout << x << endl; } inFile.close(); }

  17. Passing by Reference In C: swap(&x, &y); // call passes addresses void swap(int* x, int* y) { // ptrs receive addresses int temp; temp = *x; // dereference pointer *x = *y; // dereference pointers *y = temp; // dereference pointer }

  18. Passing by Reference (con’t) In C++: swap(x, y); // just pass variable void swap(int& x, int&y) { // x and y are references int temp; temp = x; // no dereferencing x = y; // no dereferencing y = temp; // no dereferencing }

  19. Constants #define PI 3.14 Preprocessor replaces all occurrences of PI with the value 3.14. #define actually defines a macro. const int PI = 3.14; Is not replaced by the processor. Value cannot be changed -- runtime error.

  20. Other C++ Topics • Can us endl in place of ‘\n’ • Variables can be declared anywhere in C++ code (but pick a logical place) • Output can be formatted by using stream manipulators (e.g., setw, setprecision) (Ch. 11.6) • Namespaces - now part of ANSI C++ (Ch. 21)

  21. Other Things to be Aware of (both C and C++) • in-line functions • avoids a function call by “advising” the compiler to generate a copy of the function in place of each call • faster execution, but larger object file • inline float area(int h, int w) { return h * w} • int main() . . .

  22. Other Things to be Aware of (both C and C++) (con’t) • static storage class • scope • block • function • file • class (C++ only)

More Related