introduction to c part 4
Upload: baabtracom-mentoring-partner-first-programming-school-in-india
Post on 11-Nov-2014
392 views
DESCRIPTION
TRANSCRIPT
![Page 1: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/1.jpg)
Recall
• What are the difference between stack and heap?
• How to allocate memory in stack? And heap?
• What are the difference between malloc() and calloc()
![Page 2: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/2.jpg)
Introduction to CFile handling in CWeek 3- day 2
![Page 3: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/3.jpg)
Relevance of File handling in C
![Page 4: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/4.jpg)
Relevance of File handling in C
Instruction Address
11011110 00110000
00010010 00110001
10000000 00001010
01001000 10000001
00001100 10000100
11000001 00101011
01011011 01011001
11101011 11111000Mother Board
Ram
Loads instruction for execution
Some data
CPU
Ram Stores data whenever the power is
on
![Page 5: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/5.jpg)
Instruction AddressRam
Loads instruction for execution
no data
CPU
Mother Board
Relevance of File handling in C
Ram looses all data whenever the power is OFF as it is volatile. So we need a
mechanism to store data permanently
![Page 6: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/6.jpg)
Instruction AddressRam
Loads instruction for execution
no data
CPU
Mother Board
Relevance of File handling in C
Keeps all data in a File and stores it on
secondary disks such as hard disk, CD etc
![Page 7: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/7.jpg)
You Should know !
Whenever we open a file it will be loaded into RAM memory .
Instruction Address
11011110 00110000
00010010 00110001
10000000 00001010
01001000 10000001
00001100 10000100
11000001 00101011
01011011 01011001
11101011 11111000Mother Board
Ram
Loads instruction for execution
Some data
CPU
![Page 8: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/8.jpg)
File Handling
• Files are created for permanent storage of data
• When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.
![Page 9: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/9.jpg)
Sample Program
![Page 10: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/10.jpg)
File Handling
FILE *p;*p=fopen(“data.txt”,”w”) fprintf(p,“hello World”);fclose();
First you need a File pointer to the memory
location where the file is loaded
![Page 11: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/11.jpg)
File Handling
FILE *p;*p=fopen(“data.txt”,”w”) fprintf(p,“hello World”);fclose();
Fopen() opens the file in different modes and returns the address
![Page 12: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/12.jpg)
File Handling
FILE *p;*p=fopen(“data.txt”,”w”) fprintf(p,“hello World”);fclose();
fprintf() is used to write in to a file same like we use
printf to write on the screen
![Page 13: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/13.jpg)
File Handling
FILE *p;*p=fopen(“data.txt”,”r”) fprintf(p,“hello World”);fclose(p);
File must be closed at the end of program using
inbuilt function fclose()
![Page 14: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/14.jpg)
File Modes
• r : reads from a file• w : overwrite to a file • a : append on a file• r+ : reads and writes. File
pointer at the beginning• w+ : reads and overwrites.• a+ : reads and appends .File
pointer at the beginning
![Page 15: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/15.jpg)
Write to file
• fprintf(p,” hello %s”,name); • // same like printf() writes characters
into a file
• fputs(“message”,p); • // same purpose of fprintf()
• fputc(‘h’,p);• // writes a single character into file
![Page 16: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/16.jpg)
Read from File
• fscanf(p,”%s”,message); • // same like scanf() reads characters
from file and assign to a variable
• fgets(message,100,p); • // same purpose of fscanf() but can
mention the number of characters to be read
• fgetc(p);• // returns the current character and
advance the file pointer once; returns EOF when file ending has reached
![Page 17: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/17.jpg)
#include <stdio.h>void main (){ FILE *fp; int c,n=0; fp = fopen("file.txt","r"); if(fp == NULL) { printf("Error in opening file");
Exit(0); } do { c = fgetc(fp);// fgetc() always return integer .if it is character it returns corresponding integer
printf("%c", c); }while(c != EOF); fclose(fp);}
![Page 18: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/18.jpg)
Questions?“A good question deserve a
good grade…”
![Page 19: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/19.jpg)
Self Check !!
![Page 20: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/20.jpg)
Self Check
• The first and second arguments of fopen area) A character string containing the name of the file & the second argument is the mode.b) A character string containing the name of the user & the second argument is the mode.c) A character string containing file poniter & the second argument is the mode.d) None of the mentioned of the mentioned
![Page 21: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/21.jpg)
Self Check
• The first and second arguments of fopen area) A character string containing the name of the file & the second argument is the mode.b) A character string containing the name of the user & the second argument is the mode.c) A character string containing file poniter & the second argument is the mode.d) None of the mentioned of the mentioned
![Page 22: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/22.jpg)
Self Check
• If there is any error while opening a file, fopen will returna) Nothingb) EOFc) NULLd) Depends on compiler
![Page 23: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/23.jpg)
Self Check
• If there is any error while opening a file, fopen will returna) Nothingb) EOFc) NULLd) Depends on compiler
![Page 24: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/24.jpg)
Self Check
• FILE is of type ______ ?a) int typeb) char * typec) struct typed) None of the mentioned
![Page 25: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/25.jpg)
Self Check
• FILE is of type ______ ?a) int typeb) char * typec) struct typed) None of the mentioned
![Page 26: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/26.jpg)
Self Check
• Which of the following mode argument is used to truncate?a) ab) fc) wd) t
![Page 27: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/27.jpg)
Self Check
• Which of the following mode argument is used to truncate?a) ab) fc) wd) t
![Page 28: Introduction to c part 4](https://reader033.vdocument.in/reader033/viewer/2022061115/5461deceb1af9fba388b4b66/html5/thumbnails/28.jpg)
End of Day 2