An Overview of C Files for Programmers

c fileC is one of the most widely used programming languages. The C language has a lot of applications that include being used to develop system software, since this powerful language is mapped efficiently with machine instructions.

A file is a collection or stream of byte data. The stream of data that is stored can be a document, a graphic image or any other collection of information, that is determined based on the structure of a file written.

Learn the basics of C programming at Udemy.com

The following are the two widely used types of files by the programmers.

  1. ASCII Text –

These are the most common type of text files used by the programmers. An ASCII text file is a collection or sequence of characters. In C language these files can only be processed in the forward direction. The programmers often use this file format with only one operation (read, write or append) at a time.

  1. Binary File –

The binary file is also a collection of information stored in the form of bytes. According to the design of C language, the byte and character are equivalent only. A binary file is not processed by the C language i.e., no specific processing is done on the byte when it is transferred to and fro from the file

File Handling in C:

A file in C is accessed using the FILE pointer.

FILE *fp;

There are a lot of file handling functions available in C. A few common file handling functions:

  1. fopen()
  2. fclose()
  3. fseek()
  4. ftell()
  5. fread()
  6. fwrite()
  7. fgetc()
  8. fputc()

Learn more about C Programming thorough a class at Udemy.com

fopen() –

This opens a file and initializes I/O stream for the file opened.

Syntax –

FILE *fp =fopen(const char *path, const char *mode);

This function accepts two parameters. While the first parameter is the file path which is to be handled, the second parameter is a mode for  the file which is to be opened. Fopen() has 6 different modes that can be used to open files.

The different file modes of fopen() are:

rFile is opened for reading only
wFile is opened for writing only (file need not exist)
aFile is opened for appending (file need not exist)
r+File is opened for both reading and writing. The cursor is set to the start of the opened file.
w+File is opened for both reading and writing, and data is overwritten.
a+File is opened for both reading and writing, and data is appended if already exists.

 

NOTE – There are modes like rb, which means that file is opened for reading mode as a binary file. This can be useful when you are trying to open files other than *.txt.

The fopen() returns a pointer for the file stream as a result, but if the user tries to access a file that doesn’t exist or tries to access a read-protected file, fopen() will return 0.

Example –

FILE *fp;

fp=fopen(“test.txt”, “a+”);

The above example denotes that the user is opening a file named test.txt with the mode a+. The C compiler will search for the file in the directory where the code is written, since the path is not specified.

fclose()

The fclose() function is used to close an open file. It also cleans the buffer associated to that particular file.

Syntax –

int fclose(FILE *stream);

The fclose() function takes a file pointer to the opened file. The return type is int, and it returns 0 if the file is closed successfully.

fseek()

This function sets the file pointer indicator in the file stream.

Syntax –

int fseek(FILE *stream, long offset, int whence);

The fseek() function accepts 3 parameters: the first parameter is the FILE pointer, the second parameter is the offset value or the number of bytes to seek, and the third parameter accepts three defined values SEEK_SET, SEEK_CUR, or SEEK_END. While SEEK_SET denotes the start position of the stream, SEEK_CUR denotes the position of the file pointer indicator currently in the stream. SEEK_END denotes the end position of the stream.

If the file pointer indicator position is set, fseek() returns 0.

ftell()

The ftell() function returns the file pointer indicator position of the given file stream.

Syntax –

long int ftell(FILE *stream);

The ftell() function accepts only one parameter, which is the file stream. The function returns the position of the indicator on success or the function will return -1L.

fread() and fwrite()

The fread() function is used to read the data from a file stream into the array whereas the fwrite function is used to write the data into the file stream from the array.

Syntax –

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

fread() and fwrite() have four parameters: the first parameter is the pointer to the array in which the data that has to be stored or written, the second pointer is the size in bytes of each element that is read, the third parameter is the number of elements that are read and the fourth parameter is the file stream from which the elements are read or the elements written.

fgetc ()

The fgetc() function is used to read a character from a file. The return type is int as fgetc() returns the ASCII value(0-255) of the character that is read from the file. If all characters are read from the file or the end of the file is reached, the fgetc() will return “EOF,” which denotes the end of file.

Syntax –

int fgetc (FILE *stream);

fgetc() accepts only one parameter which is the file stream.

Want to know more about advanced C programming? Take a class at Udemy.com

Fputc()

The fputc()  method is used to write one character at a time to the file.

Syntax –

int fputc( int c, FILE *stream );

This method accepts two parameters. The first parameter is the character value (0-255) that is written to the file and the second parameter is the file stream. The fputc() function returns the character if the character is successfully written or else it will return “EOF.”