The program you have actually written so far require the user to reenter data each time the regimen runs, due to the fact that data kept in variables and also control properties are stored in RAM and disappears as soon as the program stops running. If a regime is to retain data in between the times it runs, the must have actually a method of saving it. Data is saved in a file, i m sorry is usually stored on a computer’s disk. When the data is conserved in a file, that will remain there ~ the routine stops running. Data that is stored in a document can be climate retrieved and also used in ~ a later on time. Programmers usually refer to the process of saving data in a record as writing data to the file. As soon as a item of data is composed to a file, the is duplicated from a change in lamb to the file. The process of retrieving data from a file is well-known as reading data from the file. Once a piece of data is review from a file, it is replicated from the document into a change in RAM.

You are watching: A ________ access file is also known as a direct access file.

There’s three ways to produce programs that write data come files and also read data from files. Once a record is offered by a program, three actions must be taken.

Open the document — Opening a record creates a connection between the document and the program. Opening an output record usually creates the record on the disc and allows the program to write data to it. Opened an input record allows the routine to review data native the file.Process the record — Data is either composed to the paper (if the is an calculation file) or review from the record (if that is an intake file).Close the paper — After the program is finished using the file, the file must it is in closed. Closeup of the door a paper disconnects the file from the program.

File accessibility Methods

There are two basic ways to access data save on computer in a file: sequential access and also direct access. As soon as you job-related with a sequential accessibility file, you accessibility data native the beginning of the document to the end of the file. If you want to review a item of data that is stored at the an extremely end that the file, you need to read all of the data that comes before it — you cannot jump straight to the wanted data. This is comparable to the means cassette tape football player work. If you desire to hear to the last tune on a cassette tape, you need to either fast-forward over every one of the songs that come before it or listen to them. Over there is no way to jump directly to a specific song.

When you job-related with a random access record (also recognized as a direct accessibility file), you have the right to jump directly to any piece of data in the document without analysis the data the comes prior to it. This is similar to the way a CD player or one MP3 player works. You can jump straight to any type of song that you want to hear to.

In order because that a regime to work-related with a record on the computer’s disk, the program must create a file stream object in memory. A file stream thing is an object that is connected with a specific document and gives a method for the regime to work-related with that file. It is dubbed a “stream” object since a file can be assumed of as a currently of data.

File present objects work really much favor the cin and cout objects. A stream of data may be sent out to cout, which reasons values come be displayed on the screen. A currently of data might be read from the key-board by cin, and also stored in variables. Likewise, streams that data might be sent out to a file currently object, i beg your pardon writes the data to a file. Once data is read from a file, the data flows from the file present object that is connected with the file, right into variables.

Setting up a regimen for document Input/Output

Just together cin and also cout need the iostream document to be contained in the program, C++ paper access requires another header file. The record fstream consists of all the declarations crucial for document operations. It is consisted of with the adhering to statement:

#include

The fstream header paper defines the data varieties ofstream, ifstream, and also fstream. Before a C++ program deserve to work with a file, it must define an object of one of these data types. The object will certainly be “linked” with an actual document on the computer’s disk, and the operations that might be carry out on the paper depend on which of these 3 data varieties you pick for the file stream object. Table listed below lists and describes the file stream data types.


*

*

Creating a file Object and also Opening a File

Before data have the right to be written to or read from a file, the following things have to happen:

A document stream object should be createdThe record must be opened and also linked to the file stream object.

The following code shows an instance of opened a document for entry (reading).

ifstream inputFile;inputFile.open(“Customers.txt”);The an initial statement defines an ifstream object named inputFile. The 2nd statement calls the object’s open member function, pass the string “Customers.txt” together an argument. In this statement, the open member role opens the Customers.txt document and web links it v the inputFile object. After ~ this code executes, you will have the ability to use the inputFile thing to review data indigenous the Customers.txt file.

The adhering to code shows an instance of opening a document for calculation (writing).

ofstream outputFile;outputFile.open(“Employees.txt”);The very first statement specifies an ofstream object called outputFile. The second statement calls the object’s open member function, passing the wire “Employees.txt” together an argument. In this statement, the open up member duty creates the Employees.txt document and links it v the outputFile object. After ~ this code executes, friend will be able to use the outputFile object to compose data to the Employees.txt file. It’s vital to psychic that as soon as you call an ofstream object’s open up member function, the specified paper will it is in created. If the specified record already exists, it will be erased, and also a new record with the same name will certainly be created.

It is possible to specify a file currently object and also open a paper in one statement. Here is an example:

ifstream inputFile(“Customers.txt”);

This statement defines an ifstream object called inputFile and opens the Customer.txt file. Right here is an instance that defines an ofstream object called outputFile and also opens the Employees.txt file:

ofstream outputFile(“Employees.txt”);

Closing a File

The the opposite of opened a record is closing it. Although a program’s records are instantly closed as soon as the regime shuts down, it is a great programming practice to create statements that close them. Right here are two factors a program should close documents when the is finished utilizing them:

Most operating systems temporarily keep data in a file buffer before it is written to a file. A paper buffer is a little “holding section” the memory the file-bound data is first written to. Once the buffer is filled, every the data stored over there is composed to the file. This method improves the system’s performance. Closing a document causes any type of unsaved data that may still be organized in a buffer to be saved to that is file. This method the data will certainly be in the file if you must read it later in the exact same program.Some operating equipment limit the variety of files that might be open up at one time. When a routine closes files that room no longer being used, it will not deplete an ext of the operation system’s resources than necessary.

Calling the file currently object’s nearby member duty closes a file. Right here is one example:

inputFile.close();

Writing Data to a File

The regimen demonstrates an instance that reads strings as input from the keyboard and then write those strings come a file. The program asks the user to get in the very first names of three friends, and also then it writes those names come a document named Friends.txt. After ~ this code has actually executed, we can open the Friends.txt file using a text editor and also look at its contents.

// This regimen writes user input come a file.#include #include #include utilizing namespace std;int main() ofstream outputFile; cable name1, name2, name3;// open up an outputFile; outputFile.open("Friends.txt");// gain the name of 3 friends. Cout cout cin >> name1; cout cin >> name2; cout cin >> name3;// where the names come the file. OutputFile outputFile outputFile cout // close the record outputFile.close(); return 0;

Reading Data native a File

The >> operator not just reads user input from the cin object, but additionally data indigenous a file. Presume inputFile is an ifstream object, the following statement reflects the >> operator reading data from the record into the change name:

inputFile >> name;Let’s look at an example. The program listed below assumes the document Friends.txt exists, opens up the file, reads the names and displays castle on the screen, and also then close the door the file.

// This program reads data indigenous a file.#include #include #include making use of namespace std;int main() ifstream inputFile; string name;inputFile.open("Friends.txt"); cout inputFile >> name; // read name 1 indigenous the heat cout inputFile >> name; // review name 2 native the line cout inputFile >> name; // read name 3 from the line cout inputFile.close(); // nearby the record return 0;

Using Loops to process Files

Although some programs use files to store only tiny amounts of data, documents are typically used to hold large collections the data. When a program uses a file to write or read a huge amount of data, a loop is commonly involved.

Quite often a program need to read the components of a document without learning the variety of items that space stored in the file. For example, intend you must write a routine that displays every one of the item in a file, but you carry out not recognize how numerous items the document contains. You have the right to open the record and then use a loop to consistently read things from the document and display screen it. However, an error will occur if the program attempts to read past the end of the file. The program demands some method of discovering when the finish of the file has been got to so it will not try to read past it.

Fortunately, the >> operator not just reads data from a file, but also returns a true or false value indicating even if it is the data was properly read or not. If the operator return true, then a worth was properly read. If the operator returns false, it method that no value was read from the file.

There is a way to recognize whether the open member role successfully opened the file. ~ you contact the open up member function, you have the right to test the file stream object as if it to be a Boolean expression.

In every of the vault examples, the surname of the document that is opened up is hard-coded as a 11 cable literal into the program. In numerous cases, friend will desire the user come specify the surname of a document for the regime to open. In C++ 11, you deserve to pass a wire object together an debate to a paper stream object’s open up member function.

See more: Which Of These Is A Common Source Of Long-Term Financing For A Corporation? ?

The program below shows one example. This variation prompts the user to get in the name of the file. In line 15, the name the the user enters is save on computer in a cable object called filename. In heat 18, the filename thing is passed as an debate to the open up function.