Download presentation
Presentation is loading. Please wait.
Published byGarey Baldwin Modified over 9 years ago
1
CMSC 2021 C++ I/O and Other Topics
2
CMSC 2022 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
CMSC 2023 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
CMSC 2024 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
CMSC 2025 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
CMSC 2026 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
CMSC 2027 C++ File I/O Declare a file name variable #include ifstream input_filename_var; // input file ofstream output_filename_var; // output file
8
CMSC 2028 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
CMSC 2029 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
CMSC 20210 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
CMSC 20211 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
CMSC 20212 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
CMSC 20213 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 to use exit function }
14
CMSC 20214 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
CMSC 20215 Sample Program Using File I/O #include void main() { ifstream inFile(“numbers.dat”); int x; if (!inFile) { cerr << “Cannot open input file. Exiting.” << endl; exit(1); }
16
CMSC 20216 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
CMSC 20217 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
CMSC 20218 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
CMSC 20219 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
CMSC 20220 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
CMSC 20221 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
CMSC 20222 Other Things to be Aware of (both C and C++) (con’t) static storage class scope block function file class (C++ only)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.