C File
C File
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;
scanf("%d", &numOfEmployees);
scanf("%d", &E[i].id);
scanf("%s", E[i].name);
scanf("%s", E[i].designation);
scanf("%d", &E[i].salary);
2
SAMID ZAFAR
if (strcmp(E[i].designation, "Manager") == 0) {
} else {
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 name\n");
scanf("%s",s1.name);
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("age: %d\n",s1.age);
printf("marks:%d\n",s1.marks);
8
SAMID ZAFAR
fclose(fp);
scanf("%s",s1.name);
scanf("%d",&s1.rollno);
scanf("%d",&s1.age);
scanf("%d",&s1.marks);
fp=fopen("student.dat","wb");
fwrite(&s1,sizeof(s1),1,fp);
fclose(fp);
printf("Enter name\n");
scanf("%s",s2.name);
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);
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("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++;
}
}
12
SAMID ZAFAR
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 name\n");
scanf("%s",s[i].name);
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("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