0% found this document useful (0 votes)
80 views

Input & Output Operations On File in C

This document discusses input and output functions for file handling in C. It lists common functions like fopen(), fclose(), fprintf(), fscanf(), and provides descriptions of what each function does. It also gives examples of opening a file in different modes like read, write, append. It demonstrates reading from a file using fopen() in read mode, fgetc() to read characters from the file one by one, and fclose() to close the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Input & Output Operations On File in C

This document discusses input and output functions for file handling in C. It lists common functions like fopen(), fclose(), fprintf(), fscanf(), and provides descriptions of what each function does. It also gives examples of opening a file in different modes like read, write, append. It demonstrates reading from a file using fopen() in read mode, fgetc() to read characters from the file one by one, and fclose() to close the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

INPUT & OUTPUT OPERATIONS ON FILE IN C

BY SANTHANABHARATHI.S , DEPT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE, 1 ST YEAR.


INPUT AND OUTPUT FUNCTION OF
FILE

In this presentation, you will learn about input and output


function of file in C. You will learn to handle standard I/O in
C using fprintf(), fscanf(), fseek() etc.
FUNCTIONS FOR FILE HANDLING IN C
Function Description
fopen() create a new file or open a existing file

fclose() closes a file

getc() reads a character from a file

putc() writes a character to a file

fscanf() reads a set of data from a file

fprintf() writes a set of data to a file

getw() reads a integer from a file

putw() writes a integer to a file

fseek() set the position to desire point

ftell() gives current position in the file

rewind() set the position to the beginning point


OPENING FILE IN C
The <stdio.h> library function fopen() is used to create a new file or open an
exiting file.

General Syntax:
*fp = FILE *fopen(const char *filename, const char *mode);

Here, *fp is the FILE pointer (FILE *fp), which will hold the reference to the opened(or created)
file.
filename is the name of the file to be opened and mode specifies the purpose of opening the file.
VARIOUS MODES OF OPENING A FILE
Mode Description
"r" To read a file. Opens a file for reading. The file must exist.

"w" To write on a file. Creates an empty file for writing. If a file already exists
with same name, its content is removed and the file is considered as a new
empty file.

"a" To append data at the end of file. The file is created if it does not exist.

"r+" To read and write on an existing file. Opens a file to update both reading and
writing. The file must exist.

"w+" To create a new file for reading and writing.

"a+" To read and append data on a file.


EXAMPLE
 STEP:1
CREATE A TEXT FILE AND SAVE IT HAS “EXAMPLE.TXT”(Naming is your wish)>>>>
 STEP:2
Open codeblocks and type the coding given below
#include <stdio.h>
int main()
{
FILE *txt; //*txt is the file pointer
char s; // s is used to print the read data
txt=fopen(“example.txt","r"); // fopen() is used to open the file , read mode is used in this example as we are reading the file
while((s=fgetc(txt))!=EOF) //while loop is used to print the data from the file
{
printf("%c",s); // printing the data from file using char s
}
fclose(txt); // fclose() is used to close the file
}
OUTPUT:
THANK
YOU

You might also like