You are on page 1of 18

SAMID ZAFAR

EXPERIMENT 6
Problem statement: An array of records contains information of managers and workers of a
company. Print all the data of managers and workers in separate files.

Algorithm:

1
SAMID ZAFAR

Programming Code:
#include <stdio.h>

# include <string.h>

int main(){

struct Employee{

int id;

char name[20];

char designation[20];

int salary;

} E[10];

int i;

int numOfEmployees;

printf("Enter the number of employees: ");

scanf("%d", &numOfEmployees);

for (i =0; i < numOfEmployees; i++) {

printf("Enter the details of employee %d\n", i + 1);

printf("Enter ID: ");

scanf("%d", &E[i].id);

printf("Enter Name: ");

scanf("%s", E[i].name);

printf("Enter Designation: ");

scanf("%s", E[i].designation);

printf("Enter Salary: ");

scanf("%d", &E[i].salary);

FILE *fpManager, *fpWorker;

fpManager = fopen("Managers.txt", "w");

fpWorker = fopen("Workers.txt", "w");

2
SAMID ZAFAR

for (i = 0; i < numOfEmployees; i++) {

if (strcmp(E[i].designation, "Manager") == 0) {

fprintf(fpManager, "ID: %d\n", E[i].id);

fprintf(fpManager, "Name: %s\n", E[i].name);

fprintf(fpManager, "Designation: %s\n", E[i].designation);

fprintf(fpManager, "Salary: %d\n\n", E[i].salary);

} else {

fprintf(fpWorker, "ID: %d\n", E[i].id);

fprintf(fpWorker, "Name: %s\n", E[i].name);

fprintf(fpWorker, "Designation: %s\n", E[i].designation);

fprintf(fpWorker, "Salary: %d\n\n", E[i].salary);

fclose(fpManager);

fclose(fpWorker);

return 0;

Output:

3
SAMID ZAFAR

Learning Outcomes:

4
SAMID ZAFAR

EXPERIMENT 7
Problem statement: Write a program to copy one file to other, use command line arguments.
Algorithm:

5
SAMID ZAFAR

Programming Code:
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[]){
FILE *fp1,*fp2;
char ch;
if(argc!=3){
printf("\n insufficient argument ");
exit(0);
}
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"w");
while(!feof(fp1)){
ch=fgetc(fp1);
fputc(ch,fp2);
}
printf("\n file successfully copied ");
fclose(fp1);
fclose(fp2);
return 0;
}
Output:

Learning Outcomes:

6
SAMID ZAFAR

EXPERIMENT 8
Problem statement: Write a Program to store records of a student in student file. The data must
be store using Binary File. Read the record stored in "Student.txt" file in Binary code. Edit the record
stored in Binary File Append a record in the student file.

Algorithm:

7
SAMID ZAFAR

Programming Code:
#include <stdio.h>

int main(){

FILE *fp;

int rollno;

struct student{

int rollno;

char name[20];

int age;

int marks;

}s1,s2;

printf("Enter record of student\n");

printf("Enter name\n");

scanf("%s",s1.name);

printf("Enter roll no\n");

scanf("%d",&s1.rollno);

printf("Enter age\n");

scanf("%d",&s1.age);

printf("Enter marks\n");

scanf("%d",&s1.marks);

fp=fopen("student.dat","wb");

fwrite(&s1,sizeof(s1),1,fp);

fclose(fp);

fp=fopen("student.dat","rb");

fread(&s1,sizeof(s1),1,fp);

printf("Student Record\n");

printf("name: %s\n",s1.name);

printf("roll no: %d\n",s1.rollno);

printf("age: %d\n",s1.age);

printf("marks:%d\n",s1.marks);

8
SAMID ZAFAR

fclose(fp);

printf("Edit Student Record\n");

printf("Enter new name\n");

scanf("%s",s1.name);

printf("Enter new roll no\n");

scanf("%d",&s1.rollno);

printf("Enter new age\n");

scanf("%d",&s1.age);

printf("Enter new marks\n");

scanf("%d",&s1.marks);

fp=fopen("student.dat","wb");

fwrite(&s1,sizeof(s1),1,fp);

fclose(fp);

printf("Enter Record of Student 2\n");

printf("Enter name\n");

scanf("%s",s2.name);

printf("Enter roll no\n");

scanf("%d",&s2.rollno);

printf("Enter age\n");

scanf("%d",&s2.age);

printf("Enter marks\n");

scanf("%d",&s2.marks);

fp=fopen("student.dat","ab");

fwrite(&s2,sizeof(s2),1,fp);

fclose(fp);

fp=fopen("student.dat","rb");

fread(&s1,sizeof(s1),1,fp);

printf("Students Record\n");

printf("name: %s\n",s1.name);

printf("roll no: %d\n",s1.rollno);

9
SAMID ZAFAR

printf("age: %d\n",s1.age);

printf("marks:%d\n",s1.marks);

printf("\n");

fread(&s2,sizeof(s2),1,fp);

printf("name: %s\n",s2.name);

printf("roll no: %d\n",s2.rollno);

printf("age: %d\n",s2.age);

printf("marks:%d\n",s2.marks);

fclose(fp);

return 0;

Output:

Learning Outcomes:

10
SAMID ZAFAR

EXPERIMENT 9
Problem statement: Write a program to count the number of Lowercase, Uppercase, numbers
and special characters present in the contents of text file.

Algorithm:

11
SAMID ZAFAR

Programming Code:
#include <stdio.h>
#include <ctype.h>

int main() {
FILE * fp;
fp=fopen("textfile.txt","w");
char c;
int lowercase = 0, uppercase = 0, numbers = 0, special = 0;
while((c=getchar())!=EOF)
{
putc(c,fp);
}
fclose(fp);
fp = fopen("textfile.txt", "r");
while ((c = fgetc(fp)) != EOF) {
if (islower(c)) {
lowercase++;
} else if (isupper(c)) {
uppercase++;
}else if (isdigit(c)) {
numbers++;
} else{
special++;
}
}

printf("Number of lowercase characters: %d\n", lowercase);


printf("Number of uppercase characters: %d\n", uppercase);
printf("Number of numbers: %d\n", numbers);

12
SAMID ZAFAR

printf("Number of special characters: %d\n", special);

fclose(fp);
return 0;
}
Output:

Learning Outcomes:

13
SAMID ZAFAR

EXPERIMENT 12
Problem statement: Write a C program to store the information of 5 students using an array of
structures.

Algorithm:

14
SAMID ZAFAR

Programming Code:
#include <stdio.h>

int main(){

struct student{

int rollno;

char name[20];

int age;

int marks;

}s[5];

int i;

for(i=0;i<5;i++){

printf("Enter record of student %d\n",i+1);

printf("Enter name\n");

scanf("%s",s[i].name);

printf("Enter roll no\n");

scanf("%d",&s[i].rollno);

printf("Enter age\n");

scanf("%d",&s[i].age);

printf("Enter marks\n");

scanf("%d",&s[i].marks);

printf("\n");

for(i=0;i<5;i++){

printf("Student %d:\n",i+1);

printf("name: %s\n",s[i].name);

printf("roll no: %d\n",s[i].rollno);

printf("age: %d\n",s[i].age);

printf("marks:%d\n",s[i].marks);

} return 0;

15
SAMID ZAFAR

Output:

Learning Outcomes:

16
SAMID ZAFAR

EXPERIMENT 13
Problem statement: Write a C program to convert decimal number to binary number.
Algorithm:

17
SAMID ZAFAR

Programming Code:
#include <stdio.h>

int main(){

int n,i;

int b[8];

printf("Enter number:");

scanf("%d",&n);

for(i=0;i<8;i++){

b[i]=n%2;

n/=2;

printf("binary:");

for(i=7;i>=0;i--){

printf("%d",b[i]);

}return 0;

Output:

Learning Outcomes:

18

You might also like