Presentation is loading. Please wait.

Presentation is loading. Please wait.

File I/O with Records Lesson xx

Similar presentations


Presentation on theme: "File I/O with Records Lesson xx"— Presentation transcript:

1 File I/O with Records Lesson xx
In this module, we’ll talk about file input and output using records/structures.

2 Objectives Text files vs. binary files Writing structures on a file
Reading structures from a file Our goal is cover the following topics regarding files: text files vs. binary files, writing structures, reading structures

3 Text Files vs. Binary Files
ofstream fout;  fout.open("test.dat”); Fig. 1 ofstream fout;  fout.open("test.dat", ios::text); Fig. 2 In the previous module, we wrote data on a file text mode. With this mode, the data is easily readable by humans but not terribly efficient. Fig. 1 is an example of how we set up an output stream object and attach a file. Fig. 2 is the longer version of Fig. 1. The file mode, ios::text is the default if you don’t put anything as in Fig 1. Another way to write data on a file is called binary mode which is more efficient. The drawback is that you cannot visually look at a binary file and read it like you can a text file. When you write structures, you must use a binary file. ofstream fout;  fout.open("test.dat", ios::binary); Fig. 3

4 Program Description Allow the user to enter student records that contain a name and 3 test scores. Write each student record on to a data file. Let’s write a program to show you record/structure output. The program allows the user to enter student records that contain a name and 3 test scores. As each record is entered via the keyboard, we will write the record on to a data file.

5 Writing Records on a File Part 1
#include <fstream> using std::ofstream; #include <iostream> using std::cin; using std::cout; using std::endl; using std::ios; struct student {   char name[25];   int g[3]; }; This is part 1 of the program. We have the preprocessor directives at the beginning. Since we are reading and writing student records, we have a structure called student that contains a name and an array to keep track of the 3 grades.

6 Writing Records on a File Part 2
int main() {   student s;   char again = 'y';   ofstream fout;   fout.open("test.dat", ios::binary); s.name s.g[0] s.g[1] s.g[2] In part 2 of the code, we have the beginning of main which has our declarations. We have set up a student variable called s, a character variable called again and an ofstream object called fout. Notice that when we attach the file test.dat, we have an extra parameter in the open statement: ios::binary. Structures must be written in binary mode. At the bottom of the slide we have illustrated object s for you. It is composed of the parts: s.name, s.g[0], s.g[1] and s.g[2]. s

7 Writing Records on a File Part 3
while(again == 'y')   {     cout << "Enter name: ";     cin.getline(s.name, 25);     int i;     for (i = 0; i < 3; i++)     {       cout << endl << "Enter grade #" << (i + 1) << ": ";       cin >> s.grade[i];     } In part 3 of the code, we have set up a while loop so that the user can enter as many records as they desire. Inside the while loop, we prompt the user for the student’s name and 3 grades. When we are through with this section of code, object s might look as shown depending on the data entered. Sam Spade s.name 98 25 75 s.g[0] s.g[1] s.g[2] s

8 Writing Records on a File Part 4
  fout.write((char*)&s, sizeof(student));    cout << "Enter another record? (y/n) ";    cin >> again;    cin.ignore(128, '\n');   } ///end of while loop   fout.close();   return 0; } Part 4 of the code contains the important part of the program which is the writing of a structure on to a file. The statement that does is: fout.write ( (char*)&s, sizeof (student) ); and is highlighted in red for you. This takes the entire structure called s and writes the record on to the file. Unlike console I/O where you have to write each data member, this writes the entire structure. The other part of the code listed here prompts the user for another record. If the user enters the letter ‘y’ we go to the beginning of the while loop and read in another record into s. If the user enters any letter besides ‘y’, we exit the while loop, close the file and we are done.

9 Writing Structures fout.write((char*) &s, sizeof (student) );
(2) write member function (4) address of structure (6) structure name   fout.write((char*) &s, sizeof (student) ); (1) stream object (3) cast (5) sizeof function Let’s look in detail at the statement that writes a structure on the file: fout.write ((char *) &s, sizeof (student) ); 1) fout is the stream object. 2) .write is the member function which allows us to write structures on a file. 3) (char *) is a cast, we’ll deal with this when we do pointers. 4) &s is the address of the structure we want written on the file 5) sizeof is a function that tells you the # of bytes of the item in ( )s. 6) student is the name of the structure .

10 Record Input Part 1 #include <fstream> using std::ifstream;
#include <iostream> using std::cout; using std::endl; using std::ios; struct student {   char name[25];   int g[3]; }; In this next program, we show you how to deal with structures/records that are already written on a file. We will read in and print the records that were written on the data file from the last program. To begin, we have the preprocessor directives and then we have the structure definition. Be sure you use the same structure definition for reading as you did for writing. Otherwise, the wrong data will be read in.

11 Record Input Part 2 int main() {   student s;   ifstream fin;   fin.open("test.dat", ios::binary);   fin.read((char*)&s, sizeof(student)); In the 1st part of main, we declare the student object s and the ifstream object fin. The fin.open statement attaches the file test.dat to the stream object. Notice that you must open the file in binary mode. fin.read ((char* ) &s, sizeof (student)); reads in 1 record from test.dat and stores it in the structure variable s. This statement is called a priming read. Shown at the bottom of the slide is a picture of object s after the 1st record has been read from the file. Sam Spade s.name 98 25 75 s.g[0] s.g[1] s.g[2] s

12 Record Input Part 3 while(fin)   {     cout << s.name;     int i;     for (i = 0; i < 3; i++)       cout << " " << s.grade[i];     cout << endl;     fin.read((char*)&s, sizeof(student));   }   fin.close();   return 0; } In part 3 of the code, we have the: while (fin) loop. This allows us to process records until we reach the end of file. If it is not the end of file, we print the student’s name. We use a for loop to print out the 3 grades. The fin.read at the bottom of the loop reads in the 2nd and subsequent records from the file. Every time a record is read, we go back up to the while which checks for end of file. When there are no more records on the file, we exit the loop, close the file and we are done with the program.

13 Reading Structures fin.read ((char*) &s, sizeof (student) );
(2) read member function (4) address of structure (6) structure name   fin.read ((char*) &s, sizeof (student) ); (1) stream object (3) cast (5) sizeof function Let’s look in detail at the statement that reads a structure from the file: fin.read ((char *) &s, sizeof (student) ); 1) fin is the stream object. 2) .read is the member function that reads one structures from the file. 3) (char *) is a cast, we’ll deal with this when we do pointers. 4) &s is the address of the structure where we want the data read into 5) sizeof is a function that tells you the # of bytes of the item in ( )s. 6) student is the name of the structure .

14 Summary Text files vs. binary files Writing structures on a file
Reading structures from a file In this module, we learned about text files vs. binary files. We also dealt with structure I/O on files.


Download ppt "File I/O with Records Lesson xx"

Similar presentations


Ads by Google