590 likes | 757 Views
Chapter 11 File I/O and Data Manipulation. Spring 2014. Chapter 11 File I/O and Data Manipulation. 11.1 Data Files. Report Generated by a computer program for human consumption Usually includes title information, column headings and other formatting Data file
E N D
Chapter 11 File I/O and Data Manipulation Spring 2014
11.1 Data Files • Report • Generated by a computer program for human consumption • Usually includes title information, column headings and other formatting • Data file • Written with data only and created following a consistent layout for each record in the file • Generally used as input for our programs
11.1 Data Files • Space delimited files work well for situations where the data doesn’t contain spaces Stormy Weather 123-09-8765 9.00 46 F Willy Makit 432-89-7654 9.50 40 F Ima Nerd 239-34-3458 11.25 83 F Fish Gill 762-84-6543 6.50 35 P Comma delimited can handle all data situations, but it is more difficult to read 1,"Tim","Wheeler","Physical Plant Director",50000 2,"Mike","Hitson","Small Business Owner",25000 3,"Ralph","Carestia","Summer Term Professor",0 4,"Princess","","Game Developer",1
11.1 Data Files • All files in this chapter are text files • Binary files - discussed in Chapter 17 • Can use any text editor to create the file (notepad, etc.) • Visual Studio works fine for creating data files
11.2 File Streams • Remember - the predefined objects cin and cout are streams • cin - tied to stdin (the keyboard) • cout - tied to stdout (the screen) • There are no predefined stream objects for files • However, there are two predefined stream classes that can be used to create stream objects
11.2 File Streams • The two stream classes we will use: • ifstream and ofstream • Both found in <fstream> • ifstream class - creates an object that provides the functionality to read from a file • ofstream class - creates objects to write to files
11.2 File Streams • Notice use of the namespace below #include<fstream> usingstd::ifstream; usingstd::ofstream; intmain() { ifstream fin; ofstream fout; return 0; }
11.2 File Streams • Remember fin and fout are just variables • Many students use fin and fout exclusively as counterparts to cin and cout • This is not necessary or desirable • Like other variable names, stream objects should be descriptive of their purpose
11.2 File Streams • Another form of declaring stream objects allows passing the file name to the object during its declaration constchar FILENAME[] = "file.txt"; ifstream input("filename.txt"); ofstream output( FILENAME );
11.2 File Streams • Last option - creating a file stream object specifying data will be appended to an existing file - new information is added to the end of the file #include<fstream> usingstd::ofstream; using std::ios; intmain() { ofstream report("c:\\code\\data.txt",ios::app ); return 0; }
11.2 File Streams • If a filename is supplied in the declaration of any stream object, that file will be opened, or at least an attempt will be made to open the file • File may not open for various reasons including: • The name is misspelled • Incorrect path
11.3 Opening Files • Can also utilize a member function available through the use of stream objects • Syntax: stream.open ( filename );
11.3 Opening Files • Three modes in which you can open a file: read, write and append • Read mode is accomplished by using ifstream class • Other two modes accomplished using ofstream class
11.3 Opening Files // **************** Read mode **************** // -- Method 1 -- ifstream input( "filename.txt" ); // -- Method 2 -- ifstream fin; fin.open( filename ); // **************** Write mode **************** // -- Method 1 -- ofstream output(filename); // -- Method 2 -- ofstream fout; fout.open( "filename.txt"); // **************** Append mode **************** // -- Method 1 -- ofstream datafile("c:\\code\\data.txt",ios::app ); // -- Method 2 -- ofstream fout; fout.open( filename, ios::app );
11.3 Opening Files • When opened, current position is indicated by something we call a File Position Marker (FPM) • When opened in read and write mode, the FPM is placed at the beginning of the file
11.3 Opening Files • If opened in append mode, FPM is placed at the end of the file • File Position Marker (FPM) – also referred to by names such as file pointer and cursor • Regardless of what it’s called, the functionality remains the same
11.3 Opening Files • Opening a file in write mode destroys any existing information • Both write and append modes attempt to create the file if the one specified doesn’t exist • Opening process still fails if file could not be created
11.3 Opening Files • Many reasons could cause the file not to be created: • Specifying an incorrect directory • Not having write permissions to that location • The drive not being ready, maybe a pen drive not plugged in, etc. • Opening in read mode immediately fails if file doesn’t exist - the file will not be created under any circumstance
11.4 Checking For Successful Opening • If request to open is denied, program continues executing, unless an attempt is made to access the file - at this point your program will crash • For this reason - ALWAYS check to determine if the file opened successfully
11.4 Checking For Successful Opening • Two member functions check to see if the files are opened: • .fail function returns: true if the file did not open false if opened • .is_open returns opposite values of .fail: true if opened false if not opened
11.4 Checking For Successful Opening if ( fout.is_open() ) // File is open else // File is not open if( fout.fail() ) // File is not open else // File is open • Our preference - use .is_openmember function • More intuitive than .fail because the name of the function is more indicative of what we are trying to accomplish
11.5 Closing Files • You open a file - make sure you close it • Only attempt to close the file when it’s guaranteed the file was opened • Closing a file informs the OS we are finished with this resource
11.5 Closing Files if( fout.is_open() ) { // Perform file operations fout.close(); } else // File is not open Close a file as soon as you finish using it
11.6 Writing to Files • ofstream - provides same functionality as cout to write to a file including all formatting member functions and manipulators
11.6 Writing to Files voidPrintReport(char lname[][NAME_LENGTH], int salary[], intnum_records ) { ofstream report("report.txt" ); if ( report.is_open() ) { // Print header and title information PrintHeader( report ); for (int i = 0; i < num_records; i++ ) { report << left << setw(NAME_LENGTH + 1) << lname[i] << right << setw(8) << salary[i] << endl; } report.close(); } else { cout << "Error: Unable to open report file." << endl; }
11.6 Writing to Files • In previous example, report stream object is passed to the function PrintHeader • Anytime a stream object is passed, it must be passed by reference to avoid crashing the program
11.6 Writing to Files • Inserting into or extracting information from the stream changes the state of the stream • Always pass stream objects by reference
11.7 Reading From Files • Stream objects declared with the ifstream class can be used the same way as cin • Unlikely would know how much data exists in any given data file • Typically want to read every piece of data until the end of the file (EOF) has been reached
11.7 Reading From Files • Every file has an End of File (EOF) marker at the end • The marker is automatically placed by the OS immediately after the last piece of information
11.7 Reading From Files int ReadData( char lname[][NAME_LENGTH],int salary[] ) { int num_records = 0; ifstream data_file( "data.txt"); if ( data_file.is_open() ) { data_file >> lname[num_records] >> salary[num_records]; while ( !data_file.eof() ) { num_records++; data_file >> lname[num_records] >> salary[num_records]; } data_file.close(); } else cout << "Error: Unable to open data file."<< endl; return num_records; }
11.7 Reading From Files • ReadData function on the previous page returns number of records read from the file • This returned value is used to inform other functions of how many records are stored in the arrays • Also notice - implemented a priming read to ensure that the correct number of records were read • Priming read ensures that if the file exists, but is empty, the number of records returned from the function would remain zero
11.8 Searching • Easiest method - start at the first record comparing a value from within our arrays to a user specified target • If the value matches the target record, process the information, otherwise move to the next record • If the search examines all records without finding a match, the record didn’t exist and appropriate action should be taken • An error message to the screen might suffice
11.8 Searching • Method described in the previous slide is called a linear or sequential search • Best case - the record is found after first comparison • Worst case - desired record did not exist in our arrays • Average number of iterations would be n/2, where n is the number of elements in the array
11.8 Searching /************************************************ * Name: LinearSearch * Parameters: The data array. (Pass by Const Pointer) * The number of records. (Pass by Value) * The target. (Pass by Value) * Return: none * Purpose: Display the target information or * a message stating the record could not be found. ************************************************/ found = false Loop number of record times or until found If array(loop counter) equals target Display information found = true Increment loop counter End loop If not found Display “The value you are searching for doesn’t exist”
11.8 Searching • Another searching method - binary search – is on the average much more efficient • Requires data be ordered by key value • Premise behind the binary search: logically cut the array in half • Comparison is made to see which half the target would be located in, and it too is logically cut in half • Process continues until the target is located
11.8 Searching • Binary search requires many less iterations on average • Number of iterations to find its target is roughly the number of times we can divide the array in half, or 1 + log2n Search Comparisons - Linear and Binary
11.8 Searching /************************************************ * Name: BinarySearch * Parameters: The data array. (Pass by Const Pointer) * The number of records. (Pass by Value) * The target. (Pass by Value) * Return: none * Purpose: Display the target information or * a message stating the data could not be found. ************************************************/ found = false, left = 0, right = number of records Loop while left is less than or equal to right and found not equal to true mid = (left + right) / 2 If the target equals array(mid) Display information found = true Else if the target is greater than array(mid) left = mid + 1 Else right = mid - 1 End loop If not found Display “The value you are searching for doesn’t exist”
11.9 Sorting • Bubble sort - one of the easiest sorting algorithms to implement • As a bubble floats to the surface of a pond, the bubble sort "bubbles" largest element to the end of the array • Once an element is in place, the process restarts back at beginning of the array to bubble next largest value • Each pass compares two elements - if first element is greater than the second, the elements are swapped • Continues until the end of the array
11.9 Sorting 12 5 9 1 2 5 12 9 1 2 Compare and Swap Compare and Swap 5 9 12 1 2 5 9 1 12 2 Compare and Swap Compare and Swap 5 9 1 2 12
11.9 Sorting /****************************************************** * Name: BubbleSort * Parameters: The data array. (Pass by Pointer) * The number of records. (Pass by Value) * Return: none * Purpose: Sort an array using the bubble sort * algorithm. ****************************************************/ Loop number of records times (loop counter: pass) Loop number of records – 1 times (loop counter: i) If array(i) > array(i + 1) temp = array(i) array(i) = array(i + 1) array(i + 1) = temp End If End Loop End Loop
11.9 Sorting • Can make the bubble sort algorithm a little more efficient by making a couple of observations: • Each pass places one more element in the correct location • Therefore, no need to compare the element just placed in the correct position in any future passes • Each pass now requires one less comparison
11.9 Sorting /************************************************ * Name: BubbleSort * Parameters: The data array. (Pass by Pointer) * The number of records. (Pass by Value) * Return: none * Purpose: Sort an array using the bubble sort * algorithm. ************************************************/ Loop number of records times (loop counter: pass) Loopnumber of records – pass – 1 times (loop counter: i) If array(i) > array(i + 1) temp = array(i) array(i) = array(i + 1) array(i + 1) = temp End If End Loop End Loop
11.9 Sorting • Notice even if the array is sorted, the process continues • Use a flag to stop the processing once guaranteed the array is in order • If a complete pass is made without having to swap an element, the array is ordered • After the first pass, can determine the array is already sorted; all unnecessary future passes not be needed • This change often called a flagged bubble sort
11.9 Sorting /************************************************ * Name: BubbleSort * Parameters: The data array. (Pass by Pointer) * The number of records. (Pass by Value) * Return: none * Purpose: Sort an array using the bubble sort * algorithm. ************************************************/ sorted = false Loop number of records times (loop counter: pass) or until sorted sorted = true Loop number of records – pass – 1 times (loop counter: i) If array(i) > array(i + 1) sorted = false temp = array(i) array(i) = array(i + 1) array(i + 1) = temp End If End Loop End Loop
11.11 C – The Differences • C handles keyboard and console I/O in a totally different manner • File I/O handled the same way • No stream classes or objects are used • printf and scanf closely related to the way we read and write text files in C
11.11.1 File Pointer • Instead of file streams, C uses something often called a file pointer • Variable that holds the address of a FILE structure • Used to access the file once opened • A structure is another construct similar to what we saw with classes
11.11.1 File Pointer • A file pointer can be used to access both input and output files • To use the FILE structure include <stdio.h> FILE * fptr; FILE * report; FILE * data_file; FILE * input; FILE * output;
11.11.2 Opening Files • Six different modes for opening a file in C • Three are discussed in this chapter
11.11.2 Opening Files • fopen opens a file in the specified mode and returns the file pointer used to access the file <file-pointer> = fopen ( <filename>, <mode> ); • The <filename> and <mode> are either cStrings or string literals