The term output file is used to describe a file that data is read from.

File I/O in C

File I/O in C is very similar to Matlab. There are two main differences. One, we have to type the FILE variable. Two, we read one value (or a single line of values) at a time, whereas by default in Matlab, you may read many values at once.

The basic steps for using a File in C are always the same:

  1. Create a variable of type "FILE*".

  2. Open the file using the "fopen" function and assign the "file" to the variable.

  3. Check to make sure the file was successfully opened by checking to see if the variable == NULL. If it does, an error has occured.

  4. Use the fprintf or fscanf functions to write/read from the file. Usually these function calls are placed in a loop. In the case of reading data, usually, the data is read in and placed in an array, but sometimes we process the data "on the fly" (i.e., we do not store the data, we process it and create a result directly before reading any more data.

Example Code

Here are examples of the basic syntax for opening a file and writing to or reading from it:

        

          FILE *in_file  = fopen("name_of_file", "r"); // read only
          FILE *out_file = fopen("name_of_file", "w"); // write only
          
          // test for files not existing.
          if (in_file == NULL || out_file == NULL)
            {  
              printf("Error! Could not open file\n");
              exit(-1); // must include stdlib.h
            }
          
          // write to file vs write to screen
          fprintf(file, "this is a test %d\n", integer); // write to file

          fprintf(stdout, "this is a test %d\n", integer); // write to screen 
          printf(         "this is a test %d\n", integer); // write to screen 

          // read from file/keyboard. remember the ampersands! 
          fscanf(file, "%d %d", &int_var_1, &int_var_2); 

          fscanf(stdin, "%d %d", &int_var_1, &int_var_2); 
          scanf(        "%d %d", &int_var_1, &int_var_2);
          
        
      

Here is a comparison between reading from a file in Matlab and in C:

C Language

            
          //
          //
          // C code to read a bunch of integers from a file...
          //
          //

          int number;

          FILE* in_file = fopen("name_of_file", "r"); // read only 
        
          if (! in_file ) // equivalent to saying if ( in_file == NULL )
             { 
                printf("oops, file can't be read\n");
                exit(-1);
             }

          // attempt to read the next line and store
          // the value in the "number" variable
          while ( fscanf(file, "%d", & number ) == 1 ) 
             {
               printf("We just read %d\n", number);
             }
            
          

Matlab

            
          %
          % Matlab code to read a bunch of integers from a file...
          % using fopen and fscanf.  See the Matlab topic of textread
          % to see how to accomplish this much more easily
          %



          in_file = fopen('name_of_file', 'r'); % read only

          if (in_file == -1)
            error('oops, file can''t be read');
          end


          [number, count] = fscanf(file, '%d', 1);

          while (count == 1) % while we have read a number
            fprintf('We just read %d\n', number);

            [number, count] = fscanf(file, '%d', 1); % attempt to read the next number
          end
            
          

FGETS function: Read One Line at a Time

To read one line from a file (or the keyboard) at a time, use the fgets function.

	
	  char line[100];
	  fgets( line, 100, stdin );    // stdin - keyboard
	
      

fgets places the "\n" (newline) at the end of the line. Thus if we type in "hello", what really goes into the variable line is [ 'h', 'e', 'l', 'l', 'o', '\n', '\0' ]

fgets returns the keyword null on error. Thus we often use:

        
          char line[100];
          while ( fgets( line, 100, stdin ) != null )
            {
              fprintf("The line is: %s\n", line);
            }
        
      

The programs you have written so far require the user to reenter data each time the program runs, because data kept in variables is stored in RAM and disappears once the program stops running.

If a program is retain data between the times it runs, it must have a way of saving it. Data is saved in a file, which is usually stored on a computer's disk. Once the data is saved in a file, it will remain there after the program stops running. Data stored in a file can be then retrieved and used at a later time.

Writing data

  • ★ Programmers usually refer to the process of saving data in a file as writing data to the file.
  • ★ When a piece of data is written to a file, it is copied from a variable in RAM to the file.
  • ★ An output file is a file to which data is written.
  • ★ It is called an output file because the program stores output in it.
The term output file is used to describe a file that data is read from.

Reading data

  • ★ The process of retrieving data from a file is known as reading data from the file.
  • ★ When a piece of data is read from a file, it is copied from the file into a variable in RAM.
  • ★ An input file is a file from which data is read. It is called an input file because the program gets input from the file.
The term output file is used to describe a file that data is read from.

Filenames and File Stream Objects

Files on a disk are identified by a filename. In order for a program to work with a file on the computer's disk, the program must create a file stream object in memory.

A file stream object is an object that is associated with a specific file and provides a way for the program to work with that file. It is called a "stream" object because a file can be thought of as a stream of data.

File stream objects work very much like the cin and cout object.Just as cin and cout require the iostream file to be included in the program, C++ file access requires another header file. The file

contains all the declaration neccessary for file operations. It is included with the following statement: #include<fstream>

The <fstream> header file defines the data types ofstream, ifstream, and fstream. Before a C++ program can work with a file, it must define an object of one of these data types.

File Stream Data TypeDescription
ofstreamOutput file stream. You create an object of this data type when you want to create a file and write data to it.
ifstreamInput file stream. You create an object of this data type when you want to open an existing file and read data from it.
fstreamFile stream. Objects of this data type can be used to open files for reading, writing, or both

Writing Data to a File using Output File Stream

The term output file is used to describe a file that data is read from.

ofstreamExample.cpp

Note:

  • ★ If the output file does not exist, a new file with that name is created.
  • ★ If the output file already exists, the content of the file is erased.

Reading Data from a File using Input File Stream

The term output file is used to describe a file that data is read from.

ifstreamExample.cpp

Note:

  • ★ If the input file does not exist, open fails.

Practice Programs

Restaurant Bill

What term is used to describe a file from which data is read?

The term input file is used to describe a file that data is read from. It is called an input file because the program gets data from the file. There are always three steps that must be taken when a file is used by a program.

What is the term used to describe a file that data is read from quizlet?

The term "output file" is used to describe a file that data is read from.

What is an output file quizlet?

What is an output file? A file to which a program writes a data. It is called an output file because the program sends output to it.

When a piece of data is read from a file?

When a piece of data is read from a file, it is copied from the file into the program. Closing a file disconnects the communication between the file and the program. In Python, there is nothing that can be done if the program tries to access a file to read that does not exist.