// Lab 3 Lecture 1 // -------------------------- File Input -------------------------------------- // We will need a new library that will allow us to import and // export data from a file. For this we can use the "fstream" // library. Notice that we have included it in the program below. // // File manipulation can be tricky when you are first learning it. // It contains concepts that we have not yet learned, and for that // reason you may have better luck just memorizing the steps. // // 1. Create an input file stream object. We will use this // object to bridge the gap between our program and the // input file (ifstream = input file stream). // // ifstream name; // // 2. Open the file so that you can begin to read from it. // // name.open("file_name.txt"); // // 3. Import the data. Importing from a file is almost exactly like // importing from the console. Where we use "cin" to get data // from the console, we will use our ifstream object "name" to // import data from a file. // // name >> x; // // 4. You must close your file at the end. // // name.close(); // -------------------------- File Input Example ------------------------------ // Lets look at a simple example to help clarify what we just went over. // We need to first make a text file named "input_file_name.txt" and write // our first and last name in it. Example : Jason Constanzo // file stream library #include #include using namespace std; int main() { // variables to hold what we bring in from the file string first; string second; // declare an input file stream object named fin (file in). ifstream fin; // open the file with our newly created input file stream // object. Note the file name is a string literal so // it has double quotes around it. fin.open("input_file_name.txt"); // Read in two entries from the file. Note that we use our // input file stream object 'fin' exactly like we would // use 'cin' except instead of getting data from the user // we are retrieving it from a file. fin >> first; fin >> second; // fin >> first >> second; // We could also use this syntax // close the file fin.close(); // print what we read in from file cout << "first : " << first << endl; cout << "second : " << second << endl; /* **BEWARE** * It is important to note ifstream will import items from * the file in order, and it will keep track of where it * left off so that the next data you import will be * sequentially next to the previous piece of data imported. * Space and newline characters indicate where one piece * of data begins and ends when importing strings and numbers. * That was a mouth full so lets look at an example. * * E.g. if our input file is as follows: * * 1205 Dr Who * * The first time we import an item of data using "fin >> x;" it * will pull in the number 1205 and put it into the variable * 'x'. The next time we import something using "fin >> y;" * the string "Dr" will be put into the variable 'y'. */ // ---------------------------------------------------------------------------- return 0; }