Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Introduction to c part 4
1. 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()
4. Relevance of File handling in C
Instruction Address
11011110 00110000
00010010 00110001
10000000 00001010
01001000 10000001
00001100 10000100
11000001 00101011
01011011 01011001
11101011 11111000
Mother Board
Ram
Loads instruction for execution
CPU
Ram Stores data
whenever the power is
on
5. Instruction Address
Ram
Loads instruction for execution
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
6. Instruction Address
Ram
Loads instruction for execution
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
7. 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 11111000
Mother Board
Ram
Loads instruction for execution
CPU
8. 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.
14. 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
15. 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
16. 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
17. #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);
}
20. Self Check
• The first and second arguments of fopen
are
a) 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
21. Self Check
• The first and second arguments of fopen
are
a) 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
22. Self Check
• If there is any error while opening a file, fopen
will return
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
23. Self Check
• If there is any error while opening a file, fopen
will return
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
24. Self Check
• FILE is of type ______ ?
a) int type
b) char * type
c) struct type
d) None of the mentioned
25. Self Check
• FILE is of type ______ ?
a) int type
b) char * type
c) struct type
d) None of the mentioned
26. Self Check
• Which of the following mode argument is used
to truncate?
a) a
b) f
c) w
d) t
27. Self Check
• Which of the following mode argument is used
to truncate?
a) a
b) f
c) w
d) t