Skip to main content
Engineering LibreTexts

19.1: File Input and Output

  • Page ID
    11335
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Overview of File I/O in C++

    We need to understand how to open, read, write and close text files. The following File Input/Output terms are explained:

    Text File – A file consisting of characters from the ASCII character code set. Text files (also know an ASCII text files) contain character data. When we create a text file we usually think of it consisting of a series of lines. On each line are several characters (including spaces, punctuation, etc.) and we generally end the line with a return (this a character within the ASCII character code set). The return is also known as the new line character. You are most likely already familiar with the escape code of \n which is used within C++ to indicate a return character when used with in a literal string with the cout.

    A typical text file consisting of lines can be created by text editors (Notepad) or word processing programs (Microsoft Word). When using a word processor you must usually specify the output file as text (.txt) when saving it. Most source code files are ASCII text files with a unique file extension; such as C++ using .cpp, Pascal using .pas, Cobol using .cob, etc. Thus, most compiler/Integrated Development Environment software packages (such as the Bloodshed Dev-C++ 5 compiler/IDE) can be used to create ASCII text files.

    Filename – The name and its extension. Most operating systems have restrictions on which characters can be used in filenames. Example for MS-DOS and Windows: Lab_05.txt

    Because some operating systems do not allow spaces, we suggest that you use the underscore where needed for spacing in a filename.

    Filespec – The location of a file along with its filename. It is short for file specification. Most operating systems have a set of rules on how to specify the drive and directory (or path through several directory levels) along with the filename. Example for MS-DOS and Windows: C:\myfiles\cosc_1436\Lab_05.txt

    Because some operating systems do not allow spaces, we suggest that you use the underscore where needed when creating folders or sub-directories.

    Open – Your program requesting the operating system to let it have access to an existing file or to open a new file. Within C++ this is accomplished by including the header file: <fstream> File Input/Output is handled in C++ by using a pre-defined class of data objects, similar to the way string data type is handled. This class of objects has both data type names and functions built to specifically accomplish opening and closing a file.

    Within your program you create a local storage variable with the data type of fstream like this:

    fstream inData;

    This variable will be used to store the device token that the operating system assigns to the file being opened. Thus, opening a file uses a class member function call like this:

    inData.open("C:\\myfiles\\cosc_1436\\Lab05.txt", ios::in);

    The two parameters passed to the function are the filespec and the method that you want to use the file (in this example as input). The function provides a returning value of a device token from the operating system and it is stored in the variable named inData.

    It is considered good programming practice to determine if the file was opened properly. The device token should be a non zero value. It the operating system gives you the value of zero it was not able to open the file. The reason it usually can't open a file is because the filespec is wrong (misspelled or not typed case consistent in some operating systems) or the file is not stored in the location specified. We often test the device token by using an if then control structure with the action consisting of stopping the program if it is true that you got the zero. The first line of the if then control structure looks like this:

    if (!inData)

    Don’t be misled by the not operator. This reads "if it is true that the token stored in inData is zero". If inData is zero, noting zero is 1 or true.

    Read – Moving data from a device that has been opened into a memory location defined in your program. When reading text files that have integer or floating-point constants, the operating systems converts the text symbols to a binary number. The operator used is the extraction or read operator. An example of reading is:

    inData >> next_number

    This expression is similar to reading from the standard input device (aka the keyboard):

    cin >> next_number

    The "cin" is a predefined device token associated with the Standard Input and Output devices. For our file reading example you might say, "Go to the device identified by the token stored in the inData variable and read in the next value storing it in the next_number variable within my program".

    Write – Moving data from a memory location defined in your program to a device that has been opened. When writing integer or floating-point data types, the operating system converts the binary number into the proper text symbols. The operator used is the insertion or write operator. An example of writing is:

    outData << "Total is: " << total << endl; 

    This expression is similar to writing to the standard output device (aka the monitor):

    cout << "Total is: " << total << endl;

    The "cout" is a predefined device token associated with the Standard Input and Output devices. For our file writing example you might say, "Go to the device identified by the token stored in the outData variable and write the items listed (the string constant then the value stored in my program variable named total then the endl or new line or the return character)".

    Close – Your program requesting the operating system to release a file that was previously opened. There are two reasons to close a file. First, it releases the file and frees up the associated operation system resources. Second, if closing a file that was opened for output; it will clear the out the operating system’s buffer and insure that all of the data is physically stored in the output file. Some examples of closing files:

    inData.close();

    outData.close();

    You need to study this module in conjunction with the demo file provided.

    Demonstration Program in C++

    Creating a Folder or Sub-Folder for Source Code Files

    Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

    • Demo_Programs

    If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

    Download the Demo Program

    Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials. You may need to right click on the link and select "Save Target As" in order to download the file.

    Download from Connexions: Demo_File_IO.cpp

    Download from Connexions: Demo_File_IO_Input.txt

    After you run the program use a text editor to examine the Demo_File_IO_Output.txt file created by the program. You should see the output as: Total is: 33.3

    Definitions

    Text File
    A file consisting of characters from the ASCII character code set.
    Filename
    The name and its extenstion.
    Filespec
    The location of a file along with its filename.
    Open
    Your program requesting the operating system to let it have access to an existing file or to open a new file.
    Device Token
    A key value provided by the operating system to associate a device to your program.
    Read
    Moving data from a device that has been opened into a memory location defined in your program.
    Write
    Moving data from a memory location defined in your program to a device that has been opened.
    Close
    Your program requesting the operating system to release a file that was previously opened.

    This page titled 19.1: File Input and Output is shared under a CC BY license and was authored, remixed, and/or curated by Kenneth Leroy Busbee (OpenStax CNX) .

    • Was this article helpful?