Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 firstlastnextprevioushome richard jones 20031 COMP103A Introduction to Computer Science 1 Richard Jones G2.04

Similar presentations


Presentation on theme: "1 firstlastnextprevioushome richard jones 20031 COMP103A Introduction to Computer Science 1 Richard Jones G2.04"— Presentation transcript:

1 1 firstlastnextprevioushome richard jones 20031 COMP103A Introduction to Computer Science 1 Richard Jones G2.04 richardj@waikato.ac.nz

2 firstlastnextprevious richard jones 20032 Files Files are used to store data longer term And for data that is too big to fit in main memory

3 firstlastnextprevious richard jones 20033 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]

4 firstlastnextprevious richard jones 20034 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

5 firstlastnextprevious richard jones 20035 Buffers The CPU operates quickly The file access is relatively slow Characters to and from the program are placed in a buffer:

6 firstlastnextprevious richard jones 20036 Buffers chomp2d.exe file The program i/o takes place via the buffer program running in CPU physical file on disc

7 firstlastnextprevious richard jones 20037 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.

8 firstlastnextprevious richard jones 20038 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

9 firstlastnextprevious richard jones 20039 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; }

10 firstlastnextprevious richard jones 200310 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

11 firstlastnextprevious richard jones 200311 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; }

12 firstlastnextprevious richard jones 200312 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?

13 firstlastnextprevious richard jones 200313 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

14 firstlastnextprevious richard jones 200314 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.

15 firstlastnextprevious richard jones 200315 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

16 firstlastnextprevious richard jones 200316 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…

17 firstlastnextprevious richard jones 200317 Chomp – sequel to the return #********* ********** Please input the row and column to chomp (-1, -1 to quit): 2 3 #********* ********** ***.......

18 firstlastnextprevious richard jones 200318 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"; }

19 firstlastnextprevious richard jones 200319 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;

20 firstlastnextprevious richard jones 200320 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;

21 firstlastnextprevious richard jones 200321 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.


Download ppt "1 firstlastnextprevioushome richard jones 20031 COMP103A Introduction to Computer Science 1 Richard Jones G2.04"

Similar presentations


Ads by Google