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

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Files in C & C++

Author: Ashwini Patil

Keywords: File handling, fopen, fclose


Visual Cue
Narration
Slide 1 Welcome to the spoken-tutorial on files in C.
Slide 2 In this tutorial we will learn how,

To open a file.

To read data from a file.

To write data into a file.

Few examples.

Slide 3 To record this tutorial, I am using,

Ubuntu Operating System version 11.10,

gcc Compiler version 4.6.1.

Slide 4 Let us start with the introduction to the files.

File is a collection of data.

It can be a database, a program, a letter or anything.

We can create a file and access it using C.

Now let us see an example on file handling in C.

Point cursor to the filename

file.c

I have a written program.

Let's take a look.

Note that our filename is file.c

In this program we will create a file and write data into it.

Highlight

#include <stdio.h>

Let me explain the code now

This is our header file.

Highlight

int main()

This is our main function.
Highlight

FILE *fp;

To define a file variable we use the type FILE.

The FILE variable is defined under the header stdio.h

*fp is a pointer to the FILE variable.

It will store all the information about the file.

Like its name, status and current information.

Let us go back to our slides.

Slide 5

fp = fopen(“filename”, “mode”);

Now we will see the syntax to open a file.

Here, the fopen function opens a stream.

Then it links the file with the stream.

The filename is the name of the file that we want to open or create.

We can give the path along with the filename.

And

We can also give the extension.

Here we can give the mode of the file.

Slide 6 Let us see the types of modes :

w - creates file for read and write.

r - opens file for reading.

a – writing at the end of the file.

Highlight

fp = fopen(“Sample.txt”, “w”);

Now come back to our program.

Here, we create a Sample.txt file in write mode.

We can see that the path is given.

Our file will be created on the desktop.

Highlight

fprintf(fp,"This is just an example\n");

Then we will write the statements into the file.

"Welcome to the spoken-tutorial" and

"This is a test example"

fprintf writes output to the given output stream.

Highlight

fclose(fp);

fclose closes the file associated with the stream.
Highlught

return 0;

And this is our return statement.
Click on Save Now click on Save.
Type

Ctrl, Alt and T keys

Let us execute the program.

Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard

Type

gcc file.c -o file

Type

./file

To compile, type

gcc space file.c space -o space file

Press Enter

To execute, type

./file

Press Enter

We see the file is executed.

Now we will check it out.

Click on home folder sign Let us open the home folder.

Click on the home folder option.

Now click on the Desktop option.

Search for Sample.txt file Here is our sample.txt file.

This shows that our file is successfully created.

Double click on Sample.txt file Now let us open.

Double click on the file.

We can see the messages here.

Welcome to the Spoken Tutorial.

This is a test example.

This is how we create a file and write data into it.

To read data from file Now we will see how to read data from a file.
I have already made the program.

I will open it.

In this program we will read data from our sample.txt file and print the data on the console.

Let me explain the code now.

Highlight

#include <stdio.h>

This is our header file.
Highlight

int main()

This is our main function.
Highlight

FILE *fp

Here, a file variable and a pointer to the file variable is defined.
Highlight

char c;

Then we have declared a character variable c.
Highlight

fp = fopen("Sample.txt","r");

Here, we open the file Sample.txt in read mode.

The output is stored in fp.

Highlight

if (fp == NULL)

printf("File doesn't exist\n");

Then we check the condition.

If fp is equals to NULL.

If the condition is true, then we print the message:

"File doesn't exist."

Highlight

else

{

while (c != EOF)

Else it will check for another condition.

While c is not equal to EOF.

Here, EOF is the end of file.

It denotes the end of input.

It is a condition where no more data can be read from a data source.

Highlight

c = getc(fp);

If the condition is true, then it will display the characters from Sample.txt file, on the console.

Here, getc returns a character from a specified file or stream.

Now, it will return a character from our Sample.txt file.

Highlight

putchar(c);

putchar is used to display a character on the console.

Then it will store the characters in variable c.

Highlight

fclose(fp);

Here we close the file.
Highlight

return 0;

And this is our return statement.
Click on Save Now click on Save.
Let us execute the program.
On the terminal Come back to the terminal.
Type

gcc readfile.c -o read

Type

./read

To compile, type

gcc space readfile.c space -o space read

Now press Enter

To execute, type

./read

Highlight

Output

The output is displayed as:

Welcome to the Spoken-Tutorial.

This is a test example.

This brings us to the end of this tutorial.
Come back to our slides.
Slide 7 Let us summarize.

In this tutorial we learnt,

  • File handling.
  • To write data into a file.
  • eg. fp = fopen(“Sample.txt”, “w”);
  • To read data from a file.
  • eg. fp = fopen(“Sample.txt”, “r”);
Slide 8

Assignment

As an assignment,

Write a program to create a file TEST.

Write your name and address in the file TEST.

Then display it on the console using C program.

Slide 9

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

About the Spoken Tutorial Project

Watch the video available at the link shown below

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Slide 10

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Slide 11


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signing off.

Thank You for watching.

Contributors and Content Editors

Ashwini, Nancyvarkey, PoojaMoolya