C-and-C++/C4/File-Handling-In-C/English-timed

From Script | Spoken-Tutorial
Revision as of 12:01, 25 November 2013 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration


00.01 Welcome to the spoken-tutorial on Files in C
00.05 In this tutorial we will learn,
00.07 How to open a file.
00.09 How to read data from file
00.12 How to write data into a file
00.14 Few examples
00.17 To record this tutorial i am using, Ubuntu Operating System version 11.10,
00.23 gcc Compiler version 4.6.1
00.28 Let us start with an introduction to Files
00.31 File is a collection of data
00.34 It can be a database, a program, a letter or anything


00.39 We can create a file and access it using C.
00.43 Now, let us see an example on File handling in C.
00.48 I have a written progam, lets take a look
00.51 Note that our file name is file.c
00.55 In this program we will create a file and write data into it.
01.00 Let me explain the code now
01.03 This is our header file
01.05 This is our main function
01.07 To define a file variable we use the type File.
01.12 The File variable is define under the header stdio.h
01.18 (star) *fp is a pointer to the file variable
01.22 It will store all the information about the file like its name, status and current information.


01.30 Let us go back to our slides.
01.33 Now, we will see the syntax to open a file.
01.37 Here, the fopen function opens a stream then it link the file with the stream.
01.44 File name is the name of the file that we want to open or create.
01.49 We can give the path along with the filename and we can also give the extension.
01.56 Here we can give the mode of the file.
01.59 Let us see the type of the modes
02.02 w- creates file for read and write.
02.06 r- opens file for reading


02.09 a- writing at the end of the file.
02.12 Now, come back to our program
02.14 Here we create a sample.txt file in write mode
02.20 We can see that the path is given, our file will be created on the desktop.
02.26 Then we write the statements into the file.
02.29 Welcome to the spoken tutorial and This is an test example.
02.34 fprintf writes output to the given output stream
02.39 fclose closes the file associated with the stream.
02.43 And this is our return statement.
02.46 Now click on Save.
02.48 Let us execute the program.


02.50 Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
02.59 To compile type gcc space file.c space hyphen o space file, press Enter


03.08 To execute type (dot slash)./file . Press Enter
03.12 We see the file is executed


03.15 Now we will check it out
03.17 Let us open the home folder
03.19 Click on the home folder option.
03.22 Now click on the desktop option.
03.26 Here is our sample.txt file
03.28 This shows that our file is successfully created
03.32 Now, let us open. Double click on the file.
03.36 We can see the messages here.
03.38 Welcome to the spoken tutorial. This is an test example.
03.43 This is how, we create a file and write data into it.
03.48 Now, we will see how to read data from a file
03.52 I have already made the program. I will open it.
03.55 In this program we will read our sample.txt file and print the message on the console.
04.02 In this program we will read data from our sample.txt file and print the data on the console
04.10 Let me explain the code now.
04.12 This is our header file.
04.15 This is our main function
04.17 Here a file variable and the pointer to the file variable is defined.
04.22 Then we have declared a character variable c
04.26 Here we open the file sample.txt in read mode.
04.31 The output is stored in fp
04.34 Then we check the condition
04.36 if fp is equal to null, if the condition is true then we print the message File does not exist.
04.45 else it will check for another condition.
04.48 while c is not equal to EOF
04.53 Here EOF is a end of file


04.56 It denotes the end of input.
04.59 It is a condition where no more data can be read from a datasource
05.04 If the condition is true then it will display the characters from sample.txt file on the console.
05.13 Here getc returns a haracter from a specified file or stream
05.19 Now, it will return a character from our sample.txt file.
05.24 putchar is used to display a character on the console.
05.28 Then it will store the characters in variable C
05.32 Here we close the file
05.35 And this is our return statement.
05.37 Now click on Save.
05.39 Let us execute the program
05.41 Come back to our terminal


05.44 To compile type gcc space readfile.c space hyphen o space read, now press Enter
05.54 To execute type (dot slash)./read .
05.59 The output is displayed as Welcome to the spoken tutorial
06.02 This is an test example.


06.05 This brings us to the end of this tutorial.


06.08 Come back to our slides
06.10 Let us summarize.


06.11 In this tutorial we learnt


06.13 File handling


06.15 To write data into a file.


06.17 example fp = fopen Sample.txt comma w


06.24 To read data from a file example fp = fopen Sample.txt comma r
06.32 As an assignment,
06.33 Write a program to create a file TEST
06.36 Write your name and address in the file TEST
06.40 Then display it on the console using C program


06.44 Watch the video available at the link shown below
06.47 It summarizes the Spoken Tutorial project
06.50 If you do not have good bandwidth, you can download and watch it
06.54 The Spoken Tutorial Project Team
06.56 Conducts workshops using spoken tutorials
07.00 Gives certificates to those who pass an online test
07.04 For more details, please write to, contact@spoken-tutorial.org
07.10 Spoken Tutorial Project is a part of Talk to a Teacher project
07.14 It is supported by the National Mission on Education through ICT, MHRD, Government of India
07.21 More information on this Mission is available at the link shown below
07.26 This is Ashwini Patil from IIT Bombay signing off
07.29 Thank You for watching.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14