What are the Basic File Operations in C?

Basic File Operations in C:

It handles the data of the C file I/O function on a storage device such as Hard Disk. Some C file-handling functions are given below:

A file is declared and the data is accessed using a file pointer. It has the following syntax:

FILE *sk;

where FILE refers to the type name for the file and sk refers to file pointer

Basic File Operations in C:

C File Handling

fopen():

fopen() function is used to open a file and set the file pointer to the beginning/end of a file. It has the following syntax:

sk=fopen("file_name", "mode");

where sk refers to the file pointer
file_name refers to the name of the file to be opened
mode refers to the operation mode to access data

Some modes used in data processing are given below:




Mode Description
"w"It is used to open a file to write data or information.
"r"It is used to open a file to read data or information from the beginning of a file.
"a"It is used to open a file to add data or information at the end of an existing file.
"w+"It is used to open a file for writing. Data can be read after writing using the same mode.
"r+"It is used to open a file for reading and writing data or information.
"a+"It is used to open a file to write information at the end of a file. Data can be read after writing using the same mode.

Example:

FILE *sk, *rk;
sk=fopen("STUDENT.DAT","w");
rk=fopen("TEXT.TXT","r");

fclose():

fclose() function is used to close an active file. It has the following syntax:

fclose(sk);

where sk refers to the file pointer.

Example:

fclose(sk);
fclose(rk);

getc():

getc() function is used to read a character in a file. It has the following syntax:

ch=getc(sk);

where ch refers to the char variable
sk refers to the file pointer.

putc():

putc() function is used to write a character to a file. It has the following syntax:

putc(ch,sk);

where ch refers to the char variable
sk refers to the file pointer.

feof():

feof() function is used to locate the end of a file while reading data. It has the following syntax:

feof(sk);

where sk refers to the file pointer.

getw():

getw() function is used to read integer data from a file. It has the following syntax:

n=getw(sk);

where n refers to an integer variable.
sk refers to the file pointer

putw():

putw() function is used to write integer data to a file. It has the following syntax:

putw(n,sk);

where n refers to an integer variable.
sk refers to the file pointer

fscanf():

fscanf() function is used to read data from a file. It is just like scanf() function except that fscanf() is used to read data from the disk. It has the following syntax:

fscanf(sk, "format string", &v1,&v2,&v3,...&vn);

where sk refers to the file pointer
“format string” refers to the control string which represents the conversion specification.
v1,v2,v3,…vn refers to the variable whose values are read from the disk.

fprintf():

fprintf() function is used to write data to a file. It is just like printf() function except that fprintf() is used to write data from the disk. It has the following syntax:

fprintf(sk, "format string", v1,v2,v3,...vn);

where sk refers to the file pointer
format string refers to the control string which represents the conversion specification.
v1,v2,v3,…vn refers to the variable whose values are written to the disk.

rewind():

rewind() function is used to move the file pointer to the beginning of a file. It has the following syntax:

rewind(sk);

where sk refers to the file pointer