Computer and Information Sciences 192

Project 6. Implementation of File Access


Upon successful completion of this project, the student will be able to:
A. Create a file from a linked list of data structures,
B. Access a data file and insert the data into data structures.

Skills Assumptions:

A. Able to create data structures which incorporate pointer fields,
B. Able to create linked lists of structures,
C. Able to traverse a linked list of structures to accomplish data manipulations, searches, and output operations.


Data files (disk files) and data devices (such as printers), in C++, are handled in the same manner. That is they are all are treated as I/O devices and access is accomplished through specified port addressments. Most of the work of making linkages from the program to the port is handled by the compiler and the operating system. The only concern of the programmer is to instantiate objects of the fstream class, access the data file using the .open() member function, accessing the file using member functions, and closing the file using the .close() member function.

Referring to Figure 12 of Project 4, the generalized organization of file access can be seen. Note the declared file object uses the .open() member function with the OS file name and the access mode. The file will remain open until either the file is closed or termination of the program. The file name and access mode can be either string constants or string variables allowing a significant amount of programming flexibility.

As with the standard I/O operations, once access to the data file is established the big issue is the mode and sequence of the data being placed in the file and being extracted from the file. There in lies most of the issues with file access.

File types and access techniques:

Data files are defined as being two basic types -- text and binary. Text files are those files that are accessible directly from the system via an access utility such as type or print for MS-DOS. Binary files store numeric data in "compressed format", their binary equivalents.

C++ treats all files and input/output devices in the same manner in that it sees the data as a data stream that is as a series of bytes regardless of representation -- the interpretation is handled within the application or utility program. The following is a summary of the process of data file access:

  1. Create an object which will access an external data file the fstream (fstream.h)header file must be included. This header file contains the class definition for the file I/O data streams. The fstream object must then be declared. For example:

    #include < fstream >


    fstream sampfile; //fstream class allows
    // access for either input or output

    Establish a link between the program and the physical device or data file. This is accomplished using the .open() member function of the fstream class. For example:"k:filespec.dat",ios::out);
    accesses a text file with the OS name of filespec.dat with the access mode of ios::out (Point of Information, p. 527, lists a portion of the permissable access modes).
    This link is a series of file access examples;"k:filespec.dat", ios::binary|ios::out);

  2. accesses a binary file for output purposes, once again with the name of filespec.dat. Note that a file can not be, at the same time, a text and a binary file.
  3. Accessing the device or a text file is like retrieving or sending input or output to the default device except the fstream object is used.

    For example:
    sampfile >> intobj; // data from the file

    sampfile << floatobj; // sending data to file

    Accessing a binary file requires the use of the .read() and .write() member functions. Both functions have the same basic syntax. For example: *) ptrobj, sizeof(structure));
    where the (char *) is required syntax and structure refers to the structure type of ptrobj. The write member function syntax looks like: *) ptrobj, sizeof(structure));

  4. Terminate access to the data file. This is typically accomplished using the .close() member function. For example:
  5. sampfile.close();


fopen(), as indicated earlier, requires two arguments: the file specification and the mode. The file specification is a string which contains the path and the name of the data file to be accessed. The mode is a four character string which specifies how the file is to be accessed.

Opens the file for Reading ios::in
Opens the file for Writeing ios::out
Opens the file and Appends to it ios::app
Opens a file for Binary access ios::binary

Summary of mode values used by .open()


The usefulness of the Book Inventory Program can be enhanced by the program's ability to store data on a disk from the linked list created in Project 5. The actual implementation will be left to the programmer. It is suggested that the data from the file be automatically loaded into a linked list at the beginning of the program and saved back to the file at the end of the program. It is understood that this technique has some draw-backs in industry application, however it will suffice for this project.

Program Code

It is suggested that the data be loaded immediately after the the used views the opening screen of the program and chooses to continue. The user may be presented a prompt for the data file to be accessed. The user should be notified the file about to be loaded; file should then be loaded; then the Main Menu should be presented with the normal options. After program utilization and after the user selects Quit the updated file should be saved using the name given at the start of the program.



Actual output of the program should be similar to project 5. The new items for the user will be notifiaction of data file loading (error if data file is not accessable), prompt to save newly created/edited data file, and notification that the file is being saved. New screens, selections, or operations should be demonstrated completely. In addition the contents of the data file should be accessible from an independent text editor.

Evaluation Package

___ A. Project 6 Evaluation Page

___ B. Spacing chart for all NEW screens,

___ C. Hierarchy charts for the program system

___ D. Pseudocode for each new module

___ E. Listing of the entire program with notations of the new modules

___ F. Prepare a set of data to be entered including values to be used in sample calculations. Complete the anticipated calculations.

___ G. Output sequence, including all screens will be demonstrated, printer reports and tables shall be included in the project package.

___ H. Demonstrate the program using the previously prepared set of data and calculation outcomes.