Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Lecture-15 File I/O

Similar presentations


Presentation on theme: "C Programming Lecture-15 File I/O"— Presentation transcript:

1 C Programming Lecture-15 File I/O
$p!derLabWeb C Programming Lecture-15 File I/O

2 What is File I/O? File input/output is the term used for the taking and giving the information from or to a file. The file can be of any format. The data stored in the file or the stored data in the file is used in the program or sometimes it is manipulated.

3 Why we need it? Sometimes we need a data to be stored even after the program is terminated. The File I/O system is required to store the data or retrive the data of the file. A file can be used to store the database and latter can be used to retrive the data from it. File I/O system can be used to make a copying related programs.

4 File operations There are different file operations
Creation of a new file Opening an existing file Reading from a file Writing to a file Moving to a specific location in a file (seeking) Closing a file

5 Format of streams Text Stream Binary Stream
There are two format of streams which are:- Text Stream Each character line in a text stream may be terminated by a newline character. Text streams are used for textual data, which has a consistent appearance from one environment to another or from one machine to another. Binary Stream It is a series of bytes. Binary streams are primarily used for non-textual data, which is required to keep exact contents of the file

6 Opening a File For opening a file we have the fopen funtion which takes up two parameters. First parameter is the string which stores the path of the file and second parameter is also string which stores the mode in which the should be open. Syntax:- fopen(“filename.txt”, “r”); // opening file for reading fopen(“filename.txt”, “w”); // opening file for writing

7 Different modes, files can be opened with
Modes for Text Streams :- "r" opens a file for reading "w" opens a file for writing - and writes over all previous contents "a" opens a file for appending - writing on the end of the file Modes for Binary Streams:- To use the binary modes, we just append the b to the modes of text streams like this “rb” for reading and “wb” for writing and “ab” for appending the data.

8 FILE pointer To use the opened file in the program, we have to store it in some variable and for that we have pointer to FILE. In this we store the address of the opened file. Eg. FILE *file = fopen(“filename.txt”, “w”);

9 Closing a File Like for opening a file we have a funtion, we a function for closing a file. This function takes up one parameter, which is pointer to FILE in which the address of opened file is stored. Syntax:- fclose(File *file);

10 Writing Data to a File We have different function to write data to a file. fprintf(FILE *file, “control string”, variables, [var...] ); fwrite(void *ptr, size of each element, no. of elements, FILE *file);

11 Reading Data from a File
For reading we have different functions:- fscanf(File *file, “control string”, varibles,[var....] ); fread(void *ptr, size of each element, no. of elements, FILE *file);

12 Thank you!


Download ppt "C Programming Lecture-15 File I/O"

Similar presentations


Ads by Google