Script

From Script | Spoken-Tutorial
Revision as of 11:50, 5 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 and 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.

Slide 3 To record this tutorial, I am using,

Ubuntu Operating System version 11.04,

gcc and g++ Compiler version 4.6.1 on Ubuntu.

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.

Let us see an example on file hadling in C.

Highlight

#include <stdio.h>

I have a written program.

Let's take a look at it.

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.

Slide 5 The syntax to open a file is:

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

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.

We can give the path along with the filename.

We can give the extension as well.

Slide 6 Here, mode defines the following:

w - creates file for read and write.

r - opens file for reading.

a – writing at the end of file.

Highlight

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

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



Highlight

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

We will write the statements into the file.

Whenever we are working with input\output, we use fprintf and fscanf.

fprintf writes output to the given output stream.

Highlight

fclose(fp);

fclose closes the file associated with the stream.
Highlught

return 0;

This is our return statement.
Click on Save Click on Save.
Type

Ctrl, Alt and T keys

Now let us compile

open the terminal window by pressing Ctrl, Alt and T keys simultaneously

Type

gcc file.c -o file

Type

./file

Let us compile type

gcc file.c -o file

to execute type

./file

We see the file is executed.

Now we will check it out.

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

Click here on the home sign.

Search for Sample.txt file Now check for Sample.txt file.

Here it is.

This shows that our file is successfully created.

Double click on Sample.txt file Now let us open and check it out.

Double click on the file.

We can see the messages here.

Hello Everyone.

Welcome to the Spoken Tutorial.

Errors Now let us see some common errors which we can come across.
Delete “w” Suppose here at line no. 5.

We forgot to give the mode of file.

On the terminal

press up arrow key

Let us execute and see what happens.

Come back to the terminal.

Let us compile as before.

We see there is an error too few arguments to function fopen.

Because, the syntax for fopen is filename and the mode of file.

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

I will open it and explain the code.

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.

Then we print the message:

File does not exist.

Highlight

else

{

while (c != EOF)

else it will check for another condition.

While c is not equal to EOF.

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

It will display the characters from Sample.txt file on the console.

putchar is used to display a character on the console.

Then it will store the characters in variable c.

Highlight

putchar(c);

getc returns a character from a specified file or stream.

Here, it will return a character from Sample.txt file.

Highlight

fclose(fp);

Then we close the file.

fclose closes the file associated with the stream.

Highlight

return 0;

This is our return statement.
Click on Save Now click on Save.
Let us execute and see.
On the terminal Come back to the terminal.
Type

gcc readfile.c -o rf

Type

./rf

To compile type

gcc readfile.c -o rf

To execute type

./rf

Highlight

Output

Here the output is displayed as:

Hello Everyone.

Welcome to the Spoken-Tutorial.

Error Now let us see another common error which we can come across.
Delete

!

Suppose here we forget to put the not equal to sign.
Let us execute and see.
On the terminal Come back to the terminal.

Let us compile and execute as before.

Press Ctrl, C keys simultaneously We see an unexpected output.

This is because we gave the condition as c = EOF.

The condition is false.

It will not be able to check the condition

To stop this press Ctrl and C key simultaneously.

On the editor.

Type !

Now let us fix the error.

Come back to our program and type the not equal to sign here.

Click on Save Click on Save.
On the terminal Let us compile and execute as before.

Yes it is working.

Slide 7 Now come back to our slides.

Let us summarize.

In this tutorial we learned,

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: http://spoken-tutorial.org\NMEICT-Intro

Ashwini Patil from IIT Bombay

Thank You for joining

Contributors and Content Editors

Ashwini