C
Language
More on
Pointer & File Handling
Fill in the blanks
1.write a header file for Dynamic
memory allocation <malloc.h>
2.malloc & calloc are
same.[T/F]. False
3.malloc means Memory allocation
4.Calloc means continuous block of allocation
5.realloc means Reallocation of
Memory
6.free() means Deallocate the Memory
7.Dynamic memory allocation used in
linked list
8.static memory allocation used in
Array
9.stdlib.h header file expansion
standard library
10.Pointer to pointer points value
11.File syntax FILE *ptr;
12.
r means read mode
13.
w means write mode
14. a+ means read,write ,append mode
15. rb, wb means read and write in
binary mode
16. To create a file using function
name fopen()
17. In Read mode we can write a
data.[T/F]. False
18.In Write mode we can print a
data.[T/F]. False
19.ftell() means Printing current position in file.
20.fseek() means moving previous
position.
21.The primary advantage of using a
data file is Permanent storage of data.
Answer the following
Question
(4 marks)
1.Write a Program to write a File.
#include<stdio.h>
#include<conio.h>
main()
{
char name[40];
int age,i;
float bs;
FILE *fp;
clrscr();
fp=fopen("employee.txt","w");
if(fp==NULL)
{
printf("Cannot open file");
exit();
}
for(i=0;i<5;i++)
{
printf("\n Enter Name , age , Salary\n");
scanf("%s%d%f",&name,&age,&bs);
fprintf(fp,"%s %d %f\n",name,age,bs);
}
fclose(fp);
}
2.Write a Program to Read a File.
#include<stdio.h>
#include<conio.h>
main()
{
char name[40];
int age,i;
float bs;
FILE *fp;
clrscr();
fp=fopen("employee.txt","r");
if(fp==NULL)
{
printf("Cannot open file");
}
while(fscanf(fp,"%s %d %f",&name,&age,&bs)!=EOF)
printf("%s %d %f\n",name,age,bs);
fclose(fp);
getch();
}
No comments:
Post a Comment