Understanding File Input/Output in CS150: Essential Steps and Code Examples
This guide provides an overview of file input and output operations in the CS150 Introduction to Computer Science course. It covers essential steps such as including the `` library, declaring file pointers for input and output, and methods for opening, using, and closing files. Practical examples are included to illustrate checking for errors when opening files and utilizing input/output file pointers. Ensure you understand these concepts for handling large data files effectively, as mastering them is crucial for your assignments and future programming projects.
Understanding File Input/Output in CS150: Essential Steps and Code Examples
E N D
Presentation Transcript
Summary • Don’t forget! Assignment is due on • Wednesday, October 29, 2003. CS150 Introduction to Computer Science 1
Ifstreams and Ofstreams • So far, we have been reading user input from the keyboard. • We have also been writing the output of our programs directly to the screen. • What if we want to read in a large amount of data that is stored in a file. • What if we want to write a large amount of data and store it in a file. CS150 Introduction to Computer Science 1
Steps for External files • include<fstream> • Declare file variables (pointers) that will correspond to the files you are using • Input files type: ifstream • Output files type: ofstream • Open file • Use file for input/output • Close files CS150 Introduction to Computer Science 1
Declaring File pointers ifstream ifil; ofstream ofil; • File variables or pointers are the ways that you refer to the files you are using. • Can specify which input/output file to use. • May input from more than one file • May output to more than one file CS150 Introduction to Computer Science 1
Opening files <fileptr>.open(<filename>) • Same syntax for both input and output files • Filename may be string literal or char array variable • Example: ifstream ifil; ifil.open(“input.dat”); CS150 Introduction to Computer Science 1
Checking for errors • Should check for errors opening file if(ifil.fail()) { cout << “Error opening input file “; exit(1); } • Note: you must open files first before using them! CS150 Introduction to Computer Science 1
Using file pointers • Use input file pointer wherever you use cin • Examples: • ifil >> ch; • Output output file ptr wherever you use cout • Examples: • ofil << ch; • Make sure to use correct file pointer! CS150 Introduction to Computer Science 1
Closing files • Must do at end when file is finished ifil.close(); ofil.close(); Same syntax for input and output files CS150 Introduction to Computer Science 1
Example #include<fstream> #include<iostream> #define inFile "in.txt" int main( void ) { ifstream ifil; //input stream string name; ifil.open(inFile); if (ifil.fail()) { cout << "*** Error opening file" << endl; exit (1); } while (!ifil.eof()) { ifil >> name; cout << name << " "; } ifil.close(); return 0; } CS150 Introduction to Computer Science 1