You are on page 1of 10

CAP 126

NAME:-Vinay thakur
Reg.no:-11908213
Section:-d1901
Q1.Write a program which will store the student
information
in a file and display the information of only those
students
on screen having age greater than 20.

Ans.
#include <stdio.h>
struct student
{
char name[20];
int age;
} std[3];
int main()
{
int i, num;printf("Enter number of students: ");
scanf("%d", &num);
FILE *fptr, *fptr1;
fptr1 = fopen("E:\\c lab\\clab\\temp.txt","r");
fptr = fopen("E:\\c lab\\Clab\\temp.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
for(i = 0; i < num; ++i)
{
printf("For student%d\nEnter name: ", i+1);
scanf("%s",&std[i].name);
printf("Enter age: ");
scanf("%d", &std[i].age);fwrite(&std,sizeof(std),1,fptr);
}
fclose(fptr);
if(fptr1 == NULL)
{
printf("Error!");
exit(1);
}
while( fread(&std,sizeof(std),1,fptr1)==1)
{
if(std.age>20){
printf ("Age = %d name = %s\n", std.age, std.name);
}
}
fclose(fptr1);
return 0;
}

Q2.Make two files in which numbers should be


present,print the addition in third file .
Ans.
#include<stdio.h>
int main(){
FILE *file1,*file2, *file3;
char num1[20],num2[10];
int number1, number2,result;file1=fopen("E:\\c
lab\\Clab\\num1.txt","r");
file2=fopen("E:\\c lab\\Clab\\num2.txt","r");
file3=fopen("E:\\c lab\\Clab\\result.txt","w");
if (file3== NULL)
{
puts("Unable to open file");
exit(1);
}
while( fgets ( num1, 10,file1 ) != NULL )
{
printf( "%s\n" ,num1) ;
number1 = atoi(num1);
}
while( fgets ( num2, 10,file2 ) != NULL )
{
printf( "%s\n" ,num2) ;
number2 = atoi(num2);
}
result = number1+number2;
printf("result=%d \n",result);
fprintf(file3,"%d",result);
fclose(file1) ;
fclose(file2) ; fclose(file3);
return 0;
}
Q3.Differentiate with examples:

a.Structure and unionb.calloc( ) and malloc( )


a.answer
structure
A structure is a user-defined data type available in C that allows to
combining data items of different kinds. Structures are used to represent
a record.
*A keyword STRUCT is used to define a structure.
*Each member within a structure is assigned unique
storage
area of location.
*Altering the value of member will not affect the value
of
other member in a structure.
*Individual member can be accessed at a time.
*Several members of a structure can initialize at once.
*The size of structure is greater than or equal to the sum
of
size of its members.
Example-
struct student
{
char name[10];
int reg_id;
}s;
Union A union is a special data type available in C that
allows storing
different data types in the same memory location.
*The keyword union is used to define a union.
*Memory allocated is shared by individual members of
union.
*Altering the value of any of the member will alter
another
member values.
*Only one member can be accessed at a time.
*Only the first member of a union can be initialized.
*The size of union is equal to the size of largest member.
Example-
union student{
char name[20];
Int id;
} e;
b.answer
calloc( )-
The name calloc stands for contiguous allocation.
Calloc() returns a pointer to enough free space for an
array of N
objects of the specified size, or NULL if the request
cannot be
satisfied. The storage is initialized to zero.
Calloc() take two arguments those are: number of
blocks and size of each block.Calloc() takes little longer
than malloc()because of the extra
step of initializing the allocated memory by zero.
However, in
practice the difference in speed is very tiny and not
recognizable.
syntax of calloc():
ptr=(cast type*)calloc(n , size);
ptr=(float*)calloc(4,sizeof(float));
Allocates a contiguous block of memory large enough to
hold N
elements of size bytes each. The allocated region is
initialized to
zero.
Example-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements user want to sum: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);}
printf("Enter elements: \n");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Malloc( )-
Malloc()takes one argument that is, number of bytes.
Malloc() is faster than calloc().
syntax of malloc();
ptr=(cast type*)malloc(bytesize) ;
ptr=(int*)malloc(n*sizeof(int));
Allocates N bytes of memory. If the allocation succeeds, a
void
pointer to the allocated memory is returned. Otherwise
NULL is
returned.
Malloc takes one argument that is, number of bytes.If the
space assigned by malloc() is overrun, the results are
undefined.
Example-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements you want to sum: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: \n");for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Q4.Write a program to store three employee
details
with their date of joining using nested
structure with
function.
Answer.
include<stdio.h>
struct address
{
char city[20];
char phone[14];
};
struct employee_details {
char name[20];
int doj;
struct address add;
}emp[3];
void func(struct employee_details emp);
void main ()
{
int i;
struct employee_details emp;
printf("Enter the details of 3 employee\n");
func(emp);
}
void func(struct employee_details emp)
{
int i;
for(i=0;i<3;i++){
printf("Enter three employee information
1.Name 2.Date of join 3.city 4.phone\n");
fflush(stdin);scanf("%s %d %s
%s",&emp.name,&emp.doj,&emp.add.city,&e
mp.a
dd.phone);
}
for(i=0;i<3;i++){
printf("Printing the entered three employee
details\n\n");
for(i=0;i<3;i++){
printf("name: %s\nDate of join:%d\nCity:
%s\nPhone:
%s\n\n\n",emp.name,emp.doj,emp.add.city,
emp.a
dd.phone);
}
}}
Q5.Write a program to count the vowels and
total
characters from a file.
Answer.
#include <stdio.h>int main()
{
FILE *fptr;
int vowel,totalChar,len,i;
char text[100],ch;
fptr=fopen("C:\\Users\\hpand\\Desktop\\c
heck.txt","
r");
if(fptr==NULL)
{
printf("error while opening a file");
exit(0);
}
while(fgets (text, 100,fptr ) != NULL)
{
len = strlen(text);
for(i=0;i<len;i++)
{
ch =text[i];if( ch!=' ')
{
totalChar++;
}
if((ch=='a')||(ch=='A')||(ch=='e')||
(ch=='E')||(ch=
='i')||(ch=='I')||(ch=='o')
||(ch=='O')||(ch=='u')||(ch=='U'))
{
vowel++;
}
}
}
printf("all vowels in file =%d",vowel);
//remove null char
totalChar--;
printf("Total char in file=
%d",totalChar);fclose(fptr);
return 0;
}

You might also like