Programming language C (XII). Input/Output

Input/output with C is done using the standard library. Depending on the criterion, the following types of inputs/outputs can be distinguished:

  1. For the file: standard/explicit
  2. Depending on the format: unformatted/formatted
  3. Depending on the type of access: sequential/direct

Scanf and print features seen so far work with standard, formatted and sequential access files.

Input/output on standard files

The standard files that C handles are three: stdin for input, stdout for output and stob for error messages. These files are provided by the operating system (open) to the main program for direct use. Standard files are by default the keyboard and screen, but can be redirected through the operating system. Characters < and > on unix and two are used for this purpose. The scanf and print functions mentioned in the C library are other and more important for working with standard input/output files: getchar, putchar, gets and puts. The planas are unformatted and while the first two read or write a single character, the others handle a complete line. Its management is in the file stdio.h, as well as the other functions of input/output, and are the following:

external int getchar ();extern
int putchar (int);extern char
* gets (char*);extern int puts

In programs 1 and 2 you can see how to use the getc and gets functions. Both examples define the function that reads a line and calculate the length of the line.

Program 1. Using the getchar function.
Program 2. Using the gets function.


The concept of file for C is general, since it includes both the devices (keyboard, screen, printer,...) and the files of the auxiliary memory (disks, diskettes, tapes,...).

As in other languages, this file must be opened for the corresponding control information to be loaded in memory and consulted by the basic input/output functions of the operating system. This control information is stored in a structure called FILE (defined in the stdio.h file) when the file is opened and we will use it with a pointer, since when calling the functions on a file the reference of its FILE structure must be mentioned as an argument.

Therefore, for the use of the files, in programs C a variable of the type FILE * will be defined so that when opening them (with the function of the fops) it receives the result and can be used as a parameter in the other functions of input/output.

Table 1 shows a list of input/output functions that handle the FILE structure, showing both the parameter and result data type and a brief explanation.

All functions described in table 1, except fseek, are used in sequential access on a permanently open file for handling characters (fgetc, fputc), lines (fgets, fputs), records (fread, fwrite) or special situations (feof, ferror). If stdin or stdout is determined where fp should be placed, the function is performed on the standard input or output.

Table . Ways to use the fop function.

Readings and scripts are performed in the current position, initializing this position with the fopen function and updating it in each capture. Depending on the parameter of the fops function, in addition to selecting the current position, the operations that can be carried out are defined, as shown in table 2.

Table . Input/output functions.

Program 3 uses I/O functions for the writing of the cat program used in UNIX. This program records the files that pass as an argument, one after the other in the standard output. The arguments are handled by the variables argc and argv, as we saw in chapter 10.

Program 3. Program cat that performs chaining of files.

Direct access

As seen in the examples, the basic I/O functions of the library (fgetc, fputc, freda, fwrite, ..) facilitate sequential access, since when opening the file they use and update their current position.

To carry out direct access, the above functions must be combined with the FSE, which allows to be located in a certain position. The parameters of the fseek function are the file descriptor (type FILE *), the offset and the mode. Three shapes are distinguished, each choosing a different point to apply the displacement. Select the start of the SEEK_SET file, the current SEEK_CUR position and the end of the SEEK_END file.

Program 4 defines the “correct” function that reads or writes a fixed length field that is in a given position according to a parameter.

The ftell function can also help, which returns the current position in the shortcut. When you see how editors program the search help in program 5, we have an example of using the ftell function.

Program 4. Use of direct access.
5th program. Using fseek and ftell functions.
Eusko Jaurlaritzako Industria, Merkataritza eta Turismo Saila