You get a bonus - 1 coin for daily activity. Now you have 1 coin

5. FILES in C language

Lecture



A file is a method of storing information on a physical device. A file is a concept that applies to everything — from a file on disk to a terminal.

There are no operators for working with files in C language. All necessary actions are performed using the functions included in the standard library. They allow you to work with various devices, such as disks, printer, communication channels, etc. These devices are very different from each other. However, the file system converts them into a single abstract logical device , called a stream .

There are two types of streams in C: text (text) and binary (binary).

A text stream is a sequence of characters. When transmitting characters from the stream to the screen, some of them are not displayed (for example, a carriage return, line feed).

A binary stream is a sequence of bytes that uniquely correspond to what is on the external device.

Before reading or writing information to a file, it must be open and thus linked to the stream. This can be done using the fopen () library function. It takes the external representation of the file (for example, c: \ my_prog.txt) and associates it with the internal logical name that is used later in the program. The logical name is a pointer to the required file. It must be determined; this is done, for example, as follows:

  FILE * fp; 
 

Here FILE is the type name described in the standard header file stdio.h, fp is a pointer to the file. A call to the fopen () function in the program is performed by the expression:

  fp = fopen (file specification, "file use method"); 
 

The file specification (i.e., the file name and path to it) may, for example, have the form: "c: \\ my_prog.txt" - for the file my_prog.txt on disk with :.

The way to use the file is given by the following characters:

      r - open existing file for reading;

      w - create a new file for writing (if the file with the specified name exists, it will be overwritten);

      a - add a file (open an existing file to record information starting from the end of the file, or create a file if it does not exist);


      r + - open an existing file for reading and writing;

      w + - create a new file for reading and writing;

      a + - add or create a file with the ability to read and write;


      rb - open binary file for reading;

      wb - create a binary file for writing;

      ab - add a binary file;


      r + b - open binary file for reading and writing;

      w + b - create a binary file for reading and writing;

      a + b - add a binary file with the possibility of reading and writing;


      rt - open a text file for reading;

      wt - create a text file for writing;

      at - add text file;


      r + t - open a text file for reading and writing;

      w + t - create a text file for reading and writing;

    a + t - add text file with the ability to write and read.

If t or b mode is not specified (for example, r, w or a), then it is determined by the value of the global variable _fmode. If fmode = 0_BINARY, then files are opened in binary mode, and if _fmode = 0_TEXT - in text mode. The constants 0_BINARY and 0_TEXT are defined in the file fcntl.h.

Lines of the form r + b can be written in another form: rb +.

If an error occurs as a result of calling the fopen () function, then it returns the constant NULL.

It is recommended to use the following method of opening a file:

  if ((fp = fopen ("c: \\ my_prog.txt", "rt")) == NULL) 
       {    
           puts ("Open file failed \ n"); 
           exit (1); 
       } 
 

After you finish working with the file, it should be closed. This is done using the fclose () library function. It has the following prototype:

  int fclose (FILE * fp); 
 

Upon successful completion of the operation, the fclose () function returns a value of zero. Any other value indicates an error.

Consider other library functions used to work with files (they are all described in the stdio.h file):

1. The putc () function writes a character to a file and has the following prototype:

  int putc (int with, FILE * fp);  
 

Here fp is a pointer to the file returned by the fopen () function, c is the character to be written (the variable c is of type int, but only the low byte is used). Upon successful completion, putc () returns the written character, otherwise the constant EOF is returned. It is defined in the stdio.h file and is -1.

2. The getc () function reads a character from a file and has the following prototype:

  int getc (FILE * fp);  
 

Here, fp is a pointer to the file returned by the fopen () function. This function returns a read character. The corresponding value is of type int, but the high byte is zero. If the end of the file is reached, then getc () returns the value of EOF.

3. The feof () function defines the end of the file when reading binary data and has the following prototype:

  int feof (FILE * fp);  
 

Here, fp is a pointer to the file returned by the fopen () function. When the end of the file is reached, a nonzero value is returned, otherwise 0 is returned.

4. The fputs () function writes a string of characters to a file. It differs from the puts () function only in that the pointer to a file type variable must be written as the second parameter.

For example:

  fputs ("Ехmple", fp);  
 

If an error occurs, EOF is returned.

5. The fgets () function reads a character string from a file. It differs from the gets () function in that the maximum number of input characters plus one must be specified as the second parameter, and the pointer to a variable of file type as the third parameter. The string is read in its entirety if its length does not exceed the specified number of characters, otherwise the function returns only the specified number of characters.

Consider an example:

  fgets (string, n, fp);  
 

The function returns a pointer to the string when successful completion and a constant NULL in case of an error or reaching the end of the file.

6. The fprintf () function performs the same actions as the printf () function, but works with the file. Its difference is that the pointer to a variable of file type is set as the first parameter.

For example:

  fprintf (fp, "% x", a);  
 

7. The fscanf () function performs the same actions as the scanf () function, but works with the file. Its difference is that the pointer to a variable of file type is set as the first parameter.

For example:

  fscanf (fp, "% x", & a);  
 

When the end of the file is reached, the value EOF is returned.

8. The fseek () function allows reading and writing with random access and has the following prototype:

  int fseek (FILE * fp, long count, int access);  
 

Here fp is a pointer to the file returned by the fopen () function, count is the byte number relative to the specified starting position from which the operation will be performed, access is the way to set the starting position.

The access variable can have the following values:

      0 - the initial position is set at the beginning of the file;

      1 - the starting position is considered current;

    2 - the starting position is set at the end of the file.

On success, zero is returned; on error, a nonzero value.

9. The function ferror () allows you to check the correctness of the last operation when working with files. It has the following prototype:

  int ferror (FILE * fp);  
 

In the event of an error, a non-zero value is returned, otherwise zero is returned.

10. The remove () function deletes a file and has the following prototype:

  int remove (char * file_name);  
 

Here file_name is a pointer to a string with a file specification. Upon successful completion, zero is returned, otherwise a non-zero value is returned.

11. The rewind () function sets the current position pointer to the beginning of the file and has the following prototype:

  void rewind (FILE * fp);  
 

12. The fread () function is designed to read data blocks from a stream. Has a prototype:

  unsigned fread (void * ptr, unsigned size, unsigned n, FILE * fp); 
 

It reads n data elements, each size bytes long, from the specified input stream fp into the block pointed to by the ptr pointer. The total number of bytes read is equal to the product n * size. On successful completion, the fread () function returns the number of read data elements, on error, 0.

13. The fwrite () function is intended to write data blocks to a file. Has a prototype:

  unsigned fwrite (void * ptr, unsigned size, unsigned n, FILE * fp); 
 

It adds n data elements, each size byte long, to the specified output file fp. Data is recorded from the position pointed to by the ptr pointer. Upon successful completion of the operation, the fwrite () function returns the number of written data elements, on error, the wrong number of data elements.

There are five standard files in C language with the following logical names:

stdin
      - to enter data from a standard input stream (by default - from the keyboard);

stdout
      - to output data to the standard output stream (by default - on the display screen);

stderr
      - file for displaying error messages (always associated with the display screen);

stdprn
      - to output data to the printer;

stdaus
    - for input and output data in the communication channel.

The C language also has a low-level I / O system (without buffering and data formatting) that conforms to the UNIX system standard. The prototypes of its component functions are in the file io.h. These features include:

      open () - open file;

      close () - close the file;

      read () - read data;

      write () - write data;

      lseek () - search for a specific byte in the file;

    unlink () - destroy file.
created: 2014-11-10
updated: 2021-03-13
132478



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Algorithmization and programming. Structural programming. C language

Terms: Algorithmization and programming. Structural programming. C language