Difference between revisions of "C-and-C++/C4/File-Handling-In-C/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Files in C & C++ '''Author: Ashwini Patil''' '''Keywords: File handling, fopen, fclose''' {| style="border-spacing:0;" ! <center>Visual Cue</center> !…')
 
Line 33: Line 33:
 
'''Ubuntu Operating System''' version 11.10,
 
'''Ubuntu Operating System''' version 11.10,
  
'''gcc''' '''Compiler''' version 4.6.1 on Ubuntu.
+
'''gcc''' '''Compiler''' version 4.6.1.
  
 
|-
 
|-
Line 45: Line 45:
 
We can create a '''file''' and access it using '''C'''.
 
We can create a '''file''' and access it using '''C'''.
  
Now let us see an example on '''file''' hadling in '''C'''.
+
Now let us see an example on '''file''' handling in '''C'''.
  
 
|-
 
|-
Line 93: Line 93:
  
 
'''fp = fopen(“filename”, “mode”);'''
 
'''fp = fopen(“filename”, “mode”);'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the syntax to oepn a file.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the syntax to open a file.
  
 
Here, the '''fopen''' '''function''' opens a stream.
 
Here, the '''fopen''' '''function''' opens a stream.
Line 109: Line 109:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, mode defines the following:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see the type of modes :
  
 
'''w''' - creates '''file''' for read and write.
 
'''w''' - creates '''file''' for read and write.
Line 123: Line 123:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program.
  
Here, we create a '''Sample.txt''' '''file''' in write mode.
+
Here, we create a '''Sample.txt''' '''file''' in '''write''' mode.
  
 
We can see that the path is given.
 
We can see that the path is given.
  
Our file will be created on the desktop.
+
Our file will be created on the '''desktop'''.
  
 
|-
 
|-
Line 135: Line 135:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we will write the statements into the '''file'''.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we will write the statements into the '''file'''.
  
Welcome to the spoken-tutorial and
+
'''"Welcome to the spoken-tutorial'''" and
  
This is an test example
+
'''"This is an test example"'''
  
 
'''fprintf''' writes output to the given output stream.
 
'''fprintf''' writes output to the given output stream.
Line 155: Line 155:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Click on '''Save'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''.
  
 
|-
 
|-
Line 161: Line 161:
  
 
Ctrl, Alt and T keys
 
Ctrl, Alt and T keys
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute the program.
  
Open the terminal window by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on you keyboard
+
Open the '''terminal window''' by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard
  
 
|-
 
|-
Line 173: Line 173:
  
 
'''./file'''
 
'''./file'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile, type
  
 
'''gcc file.c -o file'''
 
'''gcc file.c -o file'''
 
to execute type
 
  
 
Press '''Enter'''
 
Press '''Enter'''
 +
 +
To execute, type
  
 
'''./file'''
 
'''./file'''
 +
 +
Press '''Enter'''
  
 
|-
 
|-
Line 191: Line 193:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Click '''on home folder sign
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Click '''on home folder sign
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the home folder.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the '''home folder.
 
+
'''
Click on the home folder option.
+
Click on the '''home folder''' option.
  
Now click on the Desktop option.
+
Now click on the '''Desktop''' option.
  
 
|-
 
|-
Line 227: Line 229:
 
I will open it.
 
I will open it.
  
In this program we will read data from our '''sample.txt''' file and print the data on the console.
+
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.
 
Let me explain the code now.
Line 253: Line 255:
  
 
'''char c;'''
 
'''char c;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have declared a character variable '''c'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have declared a '''character variable c'''.
  
 
|-
 
|-
Line 259: Line 261:
  
 
'''fp = fopen("Sample.txt","r");'''
 
'''fp = fopen("Sample.txt","r");'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, we open the '''file''' '''Sample.txt''' in read mode.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, we open the '''file''' '''Sample.txt''' in '''read''' mode.
  
 
The output is stored in '''fp'''.
 
The output is stored in '''fp'''.
Line 271: Line 273:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we check the condition.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we check the condition.
  
If '''fp''' is equals to NULL.
+
If '''fp''' is equals to '''NULL'''.
  
If the condition is true.
+
If the condition is '''true''', then we print the message:
  
Then we print the message:
+
'''"File doesn't exist."'''
 
+
File doesn't exists.
+
  
 
|-
 
|-
Line 291: Line 291:
 
While '''c''' is not equal to '''EOF'''.
 
While '''c''' is not equal to '''EOF'''.
  
Here,''' EOF''' is the end of file.
+
Here,''' EOF''' is the '''end of file'''.
  
 
It denotes the end of input.
 
It denotes the end of input.
Line 301: Line 301:
  
 
'''c = getc(fp); '''
 
'''c = getc(fp); '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the condition is true.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the condition is true, then it will display the characters from '''Sample.txt''' file, on the '''console'''.
  
Then,  
+
Here, '''getc '''returns a '''character''' from a specified file or stream.
  
It will display the characters from '''Sample.txt''' file on the console.
+
Now, it will return a '''character''' from our''' Sample.txt '''file.
 
+
Here, '''getc '''returns a character from a specified file or stream.
+
 
+
Now it will return a character from''' Sample.txt '''file.
+
  
 
|-
 
|-
Line 315: Line 311:
  
 
'''putchar(c); '''
 
'''putchar(c); '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''putchar''' is used to display a character on the console.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''putchar''' is used to display a '''character''' on the '''console'''.
  
Then it will store the characters in variable '''c'''.
+
Then it will store the '''characters''' in variable '''c'''.
  
 
|-
 
|-
Line 341: Line 337:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the terminal.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the '''terminal'''.
  
 
|-
 
|-
Line 351: Line 347:
  
 
'''./read'''
 
'''./read'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile, type
  
 
'''gcc readfile.c -o read'''
 
'''gcc readfile.c -o read'''
  
'''Now press enter'''
+
Now press '''Enter'''
  
To execute type
+
To execute, type
  
 
'''./read'''
 
'''./read'''
Line 367: Line 363:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
  
Welcome to the Spoken-Tutorial.
+
'''Welcome to the Spoken-Tutorial.'''
  
This is an test example.
+
'''This is an test example.'''
  
 
|-
 
|-
Line 383: Line 379:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize.
  
In this tutorial we learned,
+
In this tutorial we learnt,
  
File handling.
+
*File handling.
  
To write data into a file.
+
*To write data into a file.
  
eg. '''fp = fopen(“Sample.txt”, “w”);'''
+
*eg. '''fp = fopen(“Sample.txt”, “w”);'''
  
To read data from a file.
+
*To read data from a file.
  
eg. '''fp = fopen(“Sample.txt”, “r”);'''
+
*eg. '''fp = fopen(“Sample.txt”, “r”);'''
  
 
|-
 
|-
Line 405: Line 401:
 
Write your name and address in the file '''TEST.'''
 
Write your name and address in the file '''TEST.'''
  
Then display it on the console using C program.
+
Then display it on the '''console''' using '''C program'''.
  
 
|-
 
|-
Line 446: Line 442:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Ashwini Patil from IIT Bombay
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is Ashwini Patil from IIT Bombay signing off.
  
Thank You for joining
+
Thank You for watching.
  
 
|}
 
|}

Revision as of 22:46, 12 December 2013

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.

We can also give the extension.

Here we can give the mode of the file.

Slide 6 Let us see the type of modes :

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”);

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 an test example"

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 Now click on Save.
Type

Ctrl, Alt and T keys

Now 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 file.c -o 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 an 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 readfile.c -o read

Now press Enter

To execute, type

./read

Highlight

Output

The output is displayed as:

Welcome to the Spoken-Tutorial.

This is an 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: 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