# Ring/Lessons/Files
## Files

In this chapter we are going to learn about files functions.
* Read
* Write
* Dir
* Rename
* Remove
* fopen
* fclose
* fflush
* freopen
* tempfile
* tempname
* fseek
* ftell
* rewind
* fgetpos
* fsetpos
* clearerr
* feof
* ferror
* perror
* fgetc
* fgets
* fputc
* fputs
* ungetc
* fread
* fwrite
* fexists

## Read Function

We can read the file content using the Read function
Syntax:
Example:
The read function can read binary files too
Example:

## Write Function

We can write string to file using the Write function
The write function can write binary data to binary files.
Syntax:
Example:

## Dir Function

We can get the folder contents (files & sub folders) using the Dir function.
Syntax:
This function returns a list and each list item is a list of two items
* File/sub folder name
* Type (0 = File , 1 = Folder/Directory)
Example:

## Rename Function

We can rename files using the Rename function
Syntax:
Example:

## Remove Function

We can delete a file using the Remove function
Syntax:
Example:

## Fopen Function

We can open a file using the Fopen function
Syntax:

## Fclose Function

When we open a file using fopen function, we can close it using the Fclose function
Syntax:

## Fflush Function

We can flushes the output buffer of a stream using the Fflush function
Syntax:

## Freopen Function

We can open another file using the same file handle and at the same file close the old file
Syntax:
Example:
/* ** Read : https://en.wikipedia.org/wiki/Device_file#Device_files ** The next code is not portable, we can use iswindows before ** using it and we can write special code for each operating system. */
freopen("CON","w",stdout) # For Microsoft Windows see "Done" + nl # print to stdout again
Output:
# Output to file : myprogoutput.txt welcome 1 2 3 4 5 6 7 8 9 10

## Tempfile Function

The function Tempfile creates a temp. file (binary).
The file will be deleted automatically when the stream is closed
Syntax:

## Tempname Function

We can generate temp. file name using the Tempname function
The generated name will be different from the name of any existing file
Syntax:

## Fseek Function

We can set the file position of the stream using the Fseek function
Syntax:
The next table presents the nWhence values

## Ftell Function

We can know the current file position of a stream using the Ftell function
Syntax:

## Rewind Function

We can set the file position to the beginning of the file using the Rewind function
Syntax:

## Fgetpos Function

We can get handle to the current file position using the Fgetpos function
Syntax:

## Fsetpos Function

We can set the current file position using the Fgetpos function
Syntax:

## Clearerr Function

We can clear the EOF error and the error indicators of a stream using the clearerr function
Syntax:

## Feof Function

We can test the end-of-file indicator using the Feof function
Syntax:

## Ferror Function

We can test the error indicator of a given stream using the Ferror function
Syntax:

## Perror Function

We can print error message to the stderr using the Perror function
Syntax:

## Fgetc Function

We can get the next character from the stream using the Fgetc function
Syntax:

## Fgets Function

We can read new line from the stream using the Fgets function
Syntax:
The function stop when nSize characters are read, new line character is read or EOF.

## Fputc Function

We can write a character to the stream using the Fputc function
Syntax:

## Fputs Function

We can write a string to the stream using the Fputs function
Syntax:

## Ungetc Function

We can push a character to the stream using the Ungetc function
The character will be available for the next read
Syntax:

## Fread Function

We can read data from a stream using the Fread function
Syntax:

## Fwrite Function

We can write data to a stream using the Fwrite function
Syntax:

## Fexists Function

We can check if a file exists using the Fexists function
Syntax:
Example:
Output:

## Example

The next program test some of the file functions