1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04
firstlastnextprevious richard jones Files Files are used to store data longer term And for data that is too big to fit in main memory
firstlastnextprevious richard jones Data streams Data streams take data to and from our programs: chomp2d.exe player requests via keyboard cin >> row >> col board display via console cout << board[row]
firstlastnextprevious richard jones File streams Streams of data don’t have to use keyboard and console: chomp2d.exe to load a board position file >> board[] to save a board position file << board[] file
firstlastnextprevious richard jones Buffers The CPU operates quickly The file access is relatively slow Characters to and from the program are placed in a buffer:
firstlastnextprevious richard jones Buffers chomp2d.exe file The program i/o takes place via the buffer program running in CPU physical file on disc
firstlastnextprevious richard jones C++ streams The header file contains object similar to cin and cout that can be used with files. The stream object in a C++ program needs to be associated with a physical file.
firstlastnextprevious richard jones Syntax for creating a file #include //header file const char FNAME[9] = "text.txt"; //const string // declare a filestream object fstream file; // associate stream with physical file file.open(FNAME, ios::out); text.txt file program physical file filestream object program.exe
firstlastnextprevious richard jones Check if it went OK! if (file == NULL) // check for success { cout << "Could not open file " << FNAME; return 1; } else { // write a message to the file file << "Hello text file\nHow are you today?"; // close the file and empty the buffer file.close(); } return 0; }
firstlastnextprevious richard jones Program output None at the console, but in the file we find: Hello text file How are you today? Hello text file How are you today? text.txt
firstlastnextprevious richard jones Getting our text back char line1[SIZE], line2[SIZE]; // to read the lines into fstream file; // declare a filestream object file.open(FNAME, ios::in); // associate stream with physical file if (file == NULL) // check for success { cout << "Could not open file " << FNAME; return 1; } else { // read a message from the file file >> line1; file >> line2; file.close(); cout << line1 << endl << line2 << endl; }
firstlastnextprevious richard jones Output of the program Hello text Process in_file finished Oops, something got lost, file streams work much like cin, it stopped reading at whitespace file >> line1; replace with?
firstlastnextprevious richard jones Getting multiword lines // read a message from the file file.getline(line1, SIZE, '\n'); file.getline(line2, SIZE, '\n'); stops reading at the end of the line
firstlastnextprevious richard jones Summary Files are collections of data on disk Streams are used to transfer data between physical file and our programs. contains the fstream object – among other things.
firstlastnextprevious richard jones ios::in & ios::out These are mode indicators (Bronson chapter 14 p 615) mode indicatormeaning ios::inopen for input ios::outopen for output ios::appopen in append mode ios::truncdelete existing file contents ios::noreplacedon’t delete existing file
firstlastnextprevious richard jones Writing char arrays to file This is a useful technique if you have to write a board game in which you have to save/load a position. Which you do…
firstlastnextprevious richard jones Chomp – sequel to the return #********* ********** Please input the row and column to chomp (-1, -1 to quit): 2 3 #********* ********** ***
firstlastnextprevious richard jones Save is automatic on quit void save_file(char board[MAXROWS][MAXCOLS]) { fstream file; file.open(FNAME, ios::out); if (file != NULL) // success, write rows to the file plus end marker { for(int r = 0; r < (MAXROWS); r++) file << board[r] << endl; file.close(); } else { cout << "File save failed"; }
firstlastnextprevious richard jones Open is optional bool load_file(char board[MAXROWS][MAXCOLS]) { char yes_no; fstream file; cout << "\nWould you like to load a saved file (y/n)? "; cin >> yes_no;
firstlastnextprevious richard jones Options carried out if( (yes_no == 'y') || (yes_no == 'Y') ) { file.open(FNAME, ios::in); // open for input if( file !=NULL) { // check status // OK read in the board as an array of strings, // NB there are no spaces in a chomp board(!). for(int r = 0; r < (MAXROWS); r++) file >> board[r]; file.close(); return true; } else cout << "File load failed"; } return false;
firstlastnextprevious richard jones For a 2D char array After you have opened the file (and checked status) Write lines as rows, terminate with newline character ‘\n’ Read data as rows, use >> if data has no spaces. Use file.getline(line, SIZE, ‘\n’) otherwise.