Monthly Archives: December 2017


Write and run an Assembly language program that converts an ASCII string containing decimal digits, stored in three consecutive locations in the memory into equivalent binary number – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code : MCSL-017
Course Title : C and Assembly Language Programming (Lab Course)
Assignment Number : MCA(1)/L-017/Assignment/17-18
Maximum Marks : 50
Weightage : 25%

Write and run an Assembly language program that converts an ASCII string containing decimal digits, stored in three consecutive locations in the memory into equivalent binary number. You may assume that the three locations contains ASCII equivalent of digit 3, digit 4 and digit 5. The output of this program should be stored in AX register  - IGNOU MCA Assignment 2017 – 18

Code:-

DATA SEGMENT
ARR DB ’3′,’4′,’5′
HUN DB ?
TEN DB ?
UNT DB ?
LEN DW $-ARR
RES DW ?
DATA ENDS

CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX

LEA SI,ARR

MOV AL,ARR[SI]
SUB AL,30H
MOV HUN,AL
INC SI

MOV AL,ARR[SI]
SUB AL,30H
MOV TEN,AL
INC SI

MOV AL,ARR[SI]
SUB AL,30H
MOV UNT,AL

MOV AX,0
MOV CL,100
MOV AL,HUN
MUL CL
MOV BX,AX

MOV AX,0
MOV CL,10
MOV AL,TEN
MUL CL

ADD AX,BX

MOV BX,0
MOV BL,UNT
ADD AX,BX

MOV RES,AX

MOV AH,4CH
INT 21H
CODE ENDS
END START

DATA SEGMENT
ARR DB '3','4','5'
HUN DB ?
TEN DB ?
UNT DB ?
LEN DW $-ARR
RES DW ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA SI,ARR
MOV AL,ARR[SI]
SUB AL,30H
MOV HUN,AL
INC SI
MOV AL,ARR[SI]
SUB AL,30H
MOV TEN,AL
INC SI
MOV AL,ARR[SI]
SUB AL,30H
MOV UNT,AL
MOV AX,0
MOV CL,100
MOV AL,HUN
MUL CL
MOV BX,AX
MOV AX,0
MOV CL,10
MOV AL,TEN
MUL CL
ADD AX,BX
MOV BX,0
MOV BL,UNT
ADD AX,BX
MOV RES,AX
MOV AH,4CH
INT 21H
CODE ENDS
END START

Screen Shots :-

Asm_program_Ascii _3digit_To_Binary

Before Execution :-

Asm_program_Ascii _3digit_To_Binary_v1

After Execution :-

Asm_program_Ascii _3digit_To_Binary_v2

Note :- To see the variable and its value you have to click vars button in the emulator.

An assembly near procedure which converts the packed BCDdigits to equivalent binary number – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code : MCSL-017
Course Title : C and Assembly Language Programming (Lab Course)
Assignment Number : MCA(1)/L-017/Assignment/17-18
Maximum Marks : 50
Weightage : 25%

Write a program in 8086 assembly Language that passes a byte containing two packed BCD digits, as parameter to anear procedure named TOBINARY, which converts the packed BCDdigits to equivalent binary number. This binary number is returned tothe calling assembly program – IGNOU MCA Assignment 2017 – 18

Code:-

DATA SEGMENT
PACKEDBCD DB 52H
BINARY DB ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX

MOV AH,0
MOV AL,PACKEDBCD

CALL TOBINARY

MOV BINARY,AL

MOV AH,4CH
INT 21H
CODE ENDS

TOBINARY PROC NEAR

MOV BL,AL

AND AL,0F0H
AND BL,0FH

MOV CL,04H
ROL AL,CL

MOV CL,10
MUL CL

ADD AL,BL
RET
TOBINARY ENDP

END START

DATA SEGMENT
PACKEDBCD DB 52H
BINARY DB ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
MOV AH,0
MOV AL,PACKEDBCD
CALL TOBINARY
MOV BINARY,AL
MOV AH,4CH
INT 21H
CODE ENDS
TOBINARY PROC NEAR
MOV BL,AL
AND AL,0F0H
AND BL,0FH
MOV CL,04H
ROL AL,CL
MOV CL,10
MUL CL
ADD AL,BL
RET
TOBINARY ENDP
END START

Screen Shots :-

Asm_program_Proc_Packed_BCD_to_Binary

Before Execution :-

Asm_program_Proc_Packed_BCD_to_Binary_v1

After Execution :-

Asm_program_Proc_Packed_BCD_to_Binary_v2

Note :- To see the variable and its value you have to click vars button in the emulator.

An Assembly language program that converts an ASCII string containing decimal digits into equivalent binary number – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code : MCSL-017
Course Title : C and Assembly Language Programming (Lab Course)
Assignment Number : MCA(1)/L-017/Assignment/17-18
Maximum Marks : 50
Weightage : 25%

Write and run an Assembly language program that converts an ASCII string containing decimal digits, stored in three consecutive locations in the memory into equivalent binary number. You may assume that the three locations contains ASCII equivalent of digit 3, digit 4 and digit 5. The output of this program should be stored in AX register – IGNOU MCA Assignment 2017 – 18

Code:-

DATA SEGMENT
MSG1 DB 10,13,’ENTER ANY STRING :- $’
MSG2 DB 10,13,’ENTERED STRING IS : $’
MSG3 DB 10,13,’ENTERED DATA IS NOT ALPABETS ! $’
MSG4 DB 10,13,’ENTERED DATA NOT A 4 DIGIT VALUE ! $’
MSG5 DB 10,13,’UPPER CASE OF STRING IS :- $’

FLAG DB 0H

P1 LABEL BYTE
M1 DB 0FFH
L1 DB ?
P11 DB 0FFH DUP (‘$’)
DATA ENDS

DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX

DISPLAY MSG1

LEA DX,P1
MOV AH,0AH
INT 21H

DISPLAY MSG2
DISPLAY P11

CHECK1: CMP L1,04H
JNE NOT4D

LEA SI,P11
MOV CH,0
MOV CL,L1
DEC CL

CHECK2:
CMP [SI],41H
JB INCFLAG
CMP [SI],7BH
JB NOINC
INCFLAG:
INC FLAG
NOINC:

INC SI
LOOP CHECK2

CHECK3: CMP FLAG,0H
JNE NOTALP

CONV:
LEA SI,P11

MOV CL,L1
MOV CH,0
CHECK4:
CMP [SI],61H
JB DONE1

CMP [SI],5BH

UPR: SUB [SI],20H

DONE1: INC SI
LOOP CHECK4

DISPLAY MSG5
DISPLAY P11

JMP DONE
NOTALP:
DISPLAY MSG3
JMP DONE

NOT4D:
DISPLAY MSG4

DONE:

MOV AH,4CH
INT 21H
CODE ENDS

END START

DATA SEGMENT
MSG1 DB 10,13,'ENTER ANY STRING :- $'
MSG2 DB 10,13,'ENTERED STRING IS : $'
MSG3 DB 10,13,'ENTERED DATA IS NOT ALPABETS ! $'
MSG4 DB 10,13,'ENTERED DATA NOT A 4 DIGIT VALUE ! $'
MSG5 DB 10,13,'UPPER CASE OF STRING IS :- $'
FLAG DB 0H
P1 LABEL BYTE
M1 DB 0FFH
L1 DB ?
P11 DB 0FFH DUP ('$')
DATA ENDS
DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
DISPLAY MSG1
LEA DX,P1
MOV AH,0AH
INT 21H
DISPLAY MSG2
DISPLAY P11
CHECK1: CMP L1,04H
JNE NOT4D
LEA SI,P11
MOV CH,0
MOV CL,L1
DEC CL
CHECK2:
CMP [SI],41H
JB INCFLAG
CMP [SI],7BH
JB NOINC
INCFLAG:
INC FLAG
NOINC:
INC SI
LOOP CHECK2
CHECK3: CMP FLAG,0H
JNE NOTALP
CONV:
LEA SI,P11
MOV CL,L1
MOV CH,0
CHECK4:
CMP [SI],61H
JB DONE1
CMP [SI],5BH
UPR: SUB [SI],20H
DONE1: INC SI
LOOP CHECK4
DISPLAY MSG5
DISPLAY P11
JMP DONE
NOTALP:
DISPLAY MSG3
JMP DONE
NOT4D:
DISPLAY MSG4
DONE:
MOV AH,4CH
INT 21H
CODE ENDS
END START

Screen Shots :-

Asm_program_4digit_2_UpperCase

After Execution :-

Asm_program_4digit_2_UpperCase_Output

Note :- To see the variable and its value you have to click vars button in the emulator.

Write a program in assembly language to reverse a given number – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code : MCSL-017
Course Title : C and Assembly Language Programming (Lab Course)
Assignment Number : MCA(1)/L-017/Assignment/17-18
Maximum Marks : 50
Weightage : 25%

Write a program in assembly language to reverse a given number – IGNOU MCA Assignment 2017 – 18

Code:-

DATA SEGMENT
NUM1 DW 12345
NUM2 DW ?
ARRY DB 10 DUP (0)
TEMP DW ?
MSG1 DB 10,13,’STORED NUMBER IN MEMORY IS : $’
MSG2 DB 10,13,’REVERSE NUMBER IS : $’
RES DB 10 DUP (‘$’)
DATA ENDS

DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX

DISPLAY MSG1

MOV AX,NUM1

LEA SI,RES
CALL HEX2DEC

LEA DX,RES
MOV AH,9
INT 21H

LEA SI,ARRY
MOV AX,NUM1

REVE:
MOV DX,0
MOV BX,10
DIV BX
MOV ARRY[SI],DL
MOV TEMP,AX
MOV AX,DX

INC SI
MOV AX,TEMP
CMP TEMP,0
JG REVE

LEA DI,ARRY
LAST:
INC DI
CMP ARRY[DI],0
JG LAST

DEC DI
MOV AL,ARRY[DI]
MOV AH,0
MOV NUM2,AX
MOV CX,10
CONV:
DEC DI
MOV AL,ARRY[DI]
MOV AH,0
MUL CX
ADD NUM2,AX

MOV AX,CX
MOV BX,10
MUL BX
MOV CX,AX

CMP ARRY[DI],0
JG CONV

DISPLAY MSG2

MOV AX,NUM2

LEA SI,RES
CALL HEX2DEC

LEA DX,RES
MOV AH,9
INT 21H

MOV AH,4CH
INT 21H
CODE ENDS

HEX2DEC PROC NEAR
MOV CX,0
MOV BX,10

LOOP1: MOV DX,0
DIV BX
ADD DL,30H
PUSH DX
INC CX
CMP AX,9
JG LOOP1

ADD AL,30H
MOV [SI],AL

LOOP2: POP AX
INC SI
MOV [SI],AL
LOOP LOOP2
RET
HEX2DEC ENDP

END START

DATA SEGMENT
NUM1 DW 12345
NUM2 DW ?
ARRY DB 10 DUP (0)
TEMP DW ?
MSG1 DB 10,13,'STORED NUMBER IN MEMORY IS : $'
MSG2 DB 10,13,'REVERSE NUMBER IS : $'
RES DB 10 DUP ('$')
DATA ENDS
DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
DISPLAY MSG1
MOV AX,NUM1
LEA SI,RES
CALL HEX2DEC
LEA DX,RES
MOV AH,9
INT 21H
LEA SI,ARRY
MOV AX,NUM1
REVE:
MOV DX,0
MOV BX,10
DIV BX
MOV ARRY[SI],DL
MOV TEMP,AX
MOV AX,DX
INC SI
MOV AX,TEMP
CMP TEMP,0
JG REVE
LEA DI,ARRY
LAST:
INC DI
CMP ARRY[DI],0
JG LAST
DEC DI
MOV AL,ARRY[DI]
MOV AH,0
MOV NUM2,AX
MOV CX,10
CONV:
DEC DI
MOV AL,ARRY[DI]
MOV AH,0
MUL CX
ADD NUM2,AX
MOV AX,CX
MOV BX,10
MUL BX
MOV CX,AX
CMP ARRY[DI],0
JG CONV
DISPLAY MSG2
MOV AX,NUM2
LEA SI,RES
CALL HEX2DEC
LEA DX,RES
MOV AH,9
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
HEX2DEC PROC NEAR
MOV CX,0
MOV BX,10
LOOP1: MOV DX,0
DIV BX
ADD DL,30H
PUSH DX
INC CX
CMP AX,9
JG LOOP1
ADD AL,30H
MOV [SI],AL
LOOP2: POP AX
INC SI
MOV [SI],AL
LOOP LOOP2
RET
HEX2DEC ENDP
END START

Screen Shots :-

Asm_program_Reverse_Number

After Execution :-

Asm_program_Reverse_Number_Output

Note :- To see the variable and its value you have to click vars button in the emulator.

Write a program in assembly language to load a byte in memory location 8000H and increment the contents of the memory location – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code : MCSL-017
Course Title : C and Assembly Language Programming (Lab Course)
Assignment Number : MCA(1)/L-017/Assignment/17-18
Maximum Marks : 50
Weightage : 25%

Write a program in assembly language to load a byte in memory location 8000H and increment the contents of the memory location – IGNOU MCA Assignment 2017 – 18

Code:-

DATA SEGMENT
NUM1 DB 7H
NUM2 DB ?
ENDS

CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX

MOV AL,NUM1

MOV [8000H],AL

INC [8000H]

MOV AL,[8000H]

MOV NUM2,AL

MOV AH,4CH
INT 21H
ENDS
END START

 

DATA SEGMENT
NUM1 DB 7H
NUM2 DB ?
ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
MOV AL,NUM1
MOV [8000H],AL
INC [8000H]
MOV AL,[8000H]
MOV NUM2,AL
MOV AH,4CH
INT 21H
ENDS
END START

Screen Shots :-

Asm_program_Load_Byte_Memory

Before Execution :-

Asm_program_Load_Byte_Memory_v1

After Execution :-

Asm_program_Load_Byte_Memory_v2

Note :- To see the variable and its value you have to click vars button in the emulator.

Write a program in assembly language to check whether a number is even or odd – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code : MCSL-017
Course Title : C and Assembly Language Programming (Lab Course)
Assignment Number : MCA(1)/L-017/Assignment/17-18
Maximum Marks : 50
Weightage : 25%

Write a program in assembly language to check whether a number is even or odd – IGNOU MCA Assignment 2017 – 18

Code:-

DATA SEGMENT
MSG1 DB 10,13,’ENTER NUMBER HERE :- $’
MSG2 DB 10,13,’ENTERED VALUE IS EVEN$’
MSG3 DB 10,13,’ENTERED VALUE IS ODD$’

DATA ENDS

DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX

DISPLAY MSG1

MOV AH,1
INT 21H
MOV AH,0

CHECK: MOV DL,2
DIV DL
CMP AH,0
JNE ODD

EVEN:
DISPLAY MSG2
JMP DONE

ODD:
DISPLAY MSG3

DONE:
MOV AH,4CH
INT 21H
CODE ENDS

END START

DATA SEGMENT
MSG1 DB 10,13,'ENTER NUMBER HERE :- $'
MSG2 DB 10,13,'ENTERED VALUE IS EVEN$'
MSG3 DB 10,13,'ENTERED VALUE IS ODD$'
DATA ENDS
DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
DISPLAY MSG1
MOV AH,1
INT 21H
MOV AH,0
CHECK: MOV DL,2
DIV DL
CMP AH,0
JNE ODD
EVEN:
DISPLAY MSG2
JMP DONE
ODD:
DISPLAY MSG3
DONE:
MOV AH,4CH
INT 21H
CODE ENDS
END START

Screen Shots :-

Asm_program_Check_Even_Odd

After Execution :-

Asm_program_Check_Even_Odd_Output

Note :- To see the variable and its value you have to click vars button in the emulator.

Write a program in assembly language to swap two numbers – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code : MCSL-017
Course Title : C and Assembly Language Programming (Lab Course)
Assignment Number : MCA(1)/L-017/Assignment/17-18
Maximum Marks : 50
Weightage : 25%

Write a program in assembly language to swap two numbers – IGNOU MCA Assignment 2017 – 18

Code:-

DATA SEGMENT
NUM1 DB 9H
NUM2 DB 7H
ENDS

CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX

MOV AL,NUM1
MOV BL,NUM2

XCHG AL,NUM2
XCHG BL,NUM1

MOV AH,4CH
INT 21H
ENDS
END START

DATA SEGMENT
NUM1 DB 9H
NUM2 DB 7H
ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
MOV AL,NUM1
MOV BL,NUM2
XCHG AL,NUM2
XCHG BL,NUM1
MOV AH,4CH
INT 21H
ENDS
END START

 

Screen Shots :-

Asm_program_XCHG_Two_Numbers

Before Execution :-

Asm_program_XCHG_Two_Numbers_Variables

After Execution :-

Asm_program_XCHG_Two_Numbers_Variables2

Note :- To see the variable and its value you have to click vars button in the emulator.

 

Write a C program to create a file containing student’s records and also give a provision to update/modify the records too – IGNOU MCA Assignment 2017 – 18

MASTER OF COMPUTER APPLICATIONS

Course Code: MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/2017-18
Maximum Marks : 100

Write a C program to create a file containing student’s records and also give a provision to update/modify the records too – IGNOU MCA Assignment 2017 – 18

Program:

#include<stdio.h>
#include<conio.h>
#include<process.h>

typedef struct student {
int roll;
char name[20];
float marks;

} stud;

void sort(int n)
{
FILE *fp_index, *fp_record;
int i, j, r1, r2, r3;
stud s1, s2, s3;

fp_index = fopen(“index.txt”, “r+b”);
fp_record = fopen(“record.txt”, “r+b”);
for (i = 0; i < n – 1; i++)
{
for (j = 0; j < n – 1; j++)
{

fseek(fp_record, sizeof(s1) * j, SEEK_SET);
fseek(fp_index, sizeof(int) * j, SEEK_SET);

fread(&r1, sizeof(int), 1, fp_index);

fread(&r2, sizeof(int), 1, fp_index);

if (r1 > r2) //swap record and index
{
fread(&s1, sizeof(s1), 1, fp_record);
fread(&s2, sizeof(s2), 1, fp_record);

fseek(fp_record, (sizeof(s1)) * j, SEEK_SET);
fseek(fp_index, (sizeof(int)) * j, SEEK_SET);
fwrite(&r2, sizeof(int), 1, fp_index);
fwrite(&r1, sizeof(int), 1, fp_index);

fwrite(&s2, sizeof(s1), 1, fp_record);
fwrite(&s1, sizeof(s1), 1, fp_record);
}
}
}

fclose(fp_index);
fclose(fp_record);

}

void add()
{
int n = 0;
float temp;
FILE *fp_index, *fp_record;
stud s1;
fp_index = fopen(“index.txt”, “r+b”);
fp_record = fopen(“record.txt”, “r+b”);

printf(“\n\nEnter roll no: “);
scanf(“%d”, &s1.roll);
printf(“\nEnter Name: “);
scanf(“%s”, s1.name);
printf(“\nEnter marks: “);
scanf(“%f”, &temp);
s1.marks = temp;

fseek(fp_record, 0, SEEK_END);

if (fwrite(&s1, sizeof(s1), 1, fp_record))
{
fseek(fp_index, 0, SEEK_END);
fwrite(&(s1.roll), sizeof(int), 1, fp_index);
}
else
{
printf(“\n\nCould NOT add !!!”);
}
rewind(fp_index);
while ((fread(&s1, sizeof(int), 1, fp_index)))
n++;

fclose(fp_index);
fclose(fp_record);

if (n > 1)
sort(n);

}

void edit(int r_n)
{
int r, loc, rec, flag = 0;
float temp;
FILE *fp_index, *fp_record;
stud s2;

fp_index = fopen(“index.txt”, “r+b”);
fp_record = fopen(“record.txt”, “r+b”);

while (fread(&r, sizeof(int), 1, fp_index))
{
if (r == r_n)
{
flag = 1;

break;
}
}
if (!flag)
{
printf(“\n\nRecord NOT found !!!”);
}
else
{
fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
loc = ftell(fp_index);

fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);

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

printf(“\n\nRoll :%d”, s2.roll);
printf(“\n\nName :%s”, s2.name);
printf(“\n\nMarks :%f”, s2.marks);

printf(“\nEnter New Name : “);
scanf(“%s”, &s2.name);
printf(“\nEnter New marks : “);
scanf(“%f”, &temp);
s2.marks = temp;

fseek(fp_record, (-1) * sizeof(s2), SEEK_CUR);

if(!(fwrite(&s2, sizeof(s2), 1, fp_record)));
printf(“\n\nCould NOT modified !!!”);

}

printf(“\n\n”);
fclose(fp_index);
fclose(fp_record);

}

void search(int r_n)
{
int r, loc, flag = 0;
float temp;
FILE *fp_index, *fp_record;
stud s2;

fp_index = fopen(“index.txt”, “r+b”);
fp_record = fopen(“record.txt”, “r+b”);

while (fread(&r, sizeof(int), 1, fp_index))
{
if (r == r_n)
{
flag = 1;

break;
}
}
if (!flag)
{
printf(“\n\nRecord NOT found !!!”);
}
else
{
fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
loc = ftell(fp_index);
fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);
fread(&s2, sizeof(s2), 1, fp_record);

printf(“\n\nRoll :%d”, s2.roll);
printf(“\n\nName :%s”, s2.name);
printf(“\n\nMarks :%f”, s2.marks);
}

printf(“\n\n”);
fclose(fp_index);
fclose(fp_record);
}

void display()
{
FILE *fp;
int flag = 1;
stud s2;
fp = fopen(“record.txt”, “rb”);

while (fread(&s2, sizeof(s2), 1, fp))
{
if ((s2.roll) != 0)
{
if (flag == 1)
printf(“\n\nRoll No Name marks”);
flag = 0;
printf(“\n\n%d”, s2.roll);
printf(” %s”, s2.name);
printf(” %f”, s2.marks);
}
}
if (flag == 1)
printf(“\n\nNo record found !!!”);
printf(“\n\n”);
fclose(fp);
}

void delet(stud *s2, int r_n)
{
FILE *temp_fp, *fp;

fp = fopen(“g.txt”, “r+b”);
temp_fp = fopen(“temp.txt”, “r+b”);

while (fread(s2, sizeof(*s2), 1, fp))
{
if ((s2->roll) != r_n)
{
fseek(temp_fp, sizeof(*s2) * ((s2->roll) – 1), 0);
fwrite(s2, sizeof(*s2), 1, temp_fp);
}
}

fclose(fp);
unlink(“g.txt”);
rename(“temp.txt”, “g.txt”);

fclose(temp_fp);
printf(“\n\n”);
}
void main()
{
FILE *fp;
stud s1, s2;
int choice, roll_no;

clrscr();

fp = fopen(“record.txt”, “a+b”);
fclose(fp);
fp = fopen(“temp.txt”, “a+b”);
fclose(fp);
fp = fopen(“index.txt”, “a+b”);
fclose(fp);

while (1)
{
printf(“\n\n1:Add 2:Display 3:Search 4:Edit 5:Delete 6:Exit \n\n”);
scanf(“%d”, &choice);
switch (choice)
{
case 1:
add();

break;
case 2:

display();

break;
case 3:
printf(“\n\nEnter Roll No To Search Record : “);
scanf(“%d”, &roll_no);
search(roll_no);

break;
case 4:
printf(“\n\nEnter Roll No To Edit Record : “);
scanf(“%d”, &roll_no);

edit(roll_no);

break;
case 5:
printf(“\n\nEnter Roll No To Delete Record : “);
scanf(“%d”, &roll_no);

delet(&s2, roll_no);

break;
case 6:
exit(0);

default:
printf(“\n\nInvalid choice !!!”);
}
}

getch();
}

Code:

#include<stdio.h>
#include<conio.h>
#include<process.h>
typedef struct student {
int roll;
char name[20];
float marks;
} stud;
void sort(int n)
{
FILE *fp_index, *fp_record;
int i, j, r1, r2, r3;
stud s1, s2, s3;
fp_index = fopen("index.txt", "r+b");
fp_record = fopen("record.txt", "r+b");
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1; j++)
{
fseek(fp_record, sizeof(s1) * j, SEEK_SET);
fseek(fp_index, sizeof(int) * j, SEEK_SET);
fread(&r1, sizeof(int), 1, fp_index);
fread(&r2, sizeof(int), 1, fp_index);
if (r1 > r2) //swap record and index
{
fread(&s1, sizeof(s1), 1, fp_record);
fread(&s2, sizeof(s2), 1, fp_record);
fseek(fp_record, (sizeof(s1)) * j, SEEK_SET);
fseek(fp_index, (sizeof(int)) * j, SEEK_SET);
fwrite(&r2, sizeof(int), 1, fp_index);
fwrite(&r1, sizeof(int), 1, fp_index);
fwrite(&s2, sizeof(s1), 1, fp_record);
fwrite(&s1, sizeof(s1), 1, fp_record);
}
}
}
fclose(fp_index);
fclose(fp_record);
}
void add()
{
int n = 0;
float temp;
FILE *fp_index, *fp_record;
stud s1;
fp_index = fopen("index.txt", "r+b");
fp_record = fopen("record.txt", "r+b");
printf("\n\nEnter roll no: ");
scanf("%d", &s1.roll);
printf("\nEnter Name: ");
scanf("%s", s1.name);
printf("\nEnter marks: ");
scanf("%f", &temp);
s1.marks = temp;
fseek(fp_record, 0, SEEK_END);
if (fwrite(&s1, sizeof(s1), 1, fp_record))
{
fseek(fp_index, 0, SEEK_END);
fwrite(&(s1.roll), sizeof(int), 1, fp_index);
}
else
{
printf("\n\nCould NOT add !!!");
}
rewind(fp_index);
while ((fread(&s1, sizeof(int), 1, fp_index)))
n++;
fclose(fp_index);
fclose(fp_record);
if (n > 1)
sort(n);
}
void edit(int r_n)
{
int r, loc, rec, flag = 0;
float temp;
FILE *fp_index, *fp_record;
stud s2;
fp_index = fopen("index.txt", "r+b");
fp_record = fopen("record.txt", "r+b");
while (fread(&r, sizeof(int), 1, fp_index))
{
if (r == r_n)
{
flag = 1;
break;
}
}
if (!flag)
{
printf("\n\nRecord NOT found !!!");
}
else
{
fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
loc = ftell(fp_index);
fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);
fread(&s2, sizeof(s2), 1, fp_record);
printf("\n\nRoll :%d", s2.roll);
printf("\n\nName :%s", s2.name);
printf("\n\nMarks :%f", s2.marks);
printf("\nEnter New Name : ");
scanf("%s", &s2.name);
printf("\nEnter New marks : ");
scanf("%f", &temp);
s2.marks = temp;
fseek(fp_record, (-1) * sizeof(s2), SEEK_CUR);
if(!(fwrite(&s2, sizeof(s2), 1, fp_record)));
printf("\n\nCould NOT modified !!!");
}
printf("\n\n");
fclose(fp_index);
fclose(fp_record);
}
void search(int r_n)
{
int r, loc, flag = 0;
float temp;
FILE *fp_index, *fp_record;
stud s2;
fp_index = fopen("index.txt", "r+b");
fp_record = fopen("record.txt", "r+b");
while (fread(&r, sizeof(int), 1, fp_index))
{
if (r == r_n)
{
flag = 1;
break;
}
}
if (!flag)
{
printf("\n\nRecord NOT found !!!");
}
else
{
fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
loc = ftell(fp_index);
fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);
fread(&s2, sizeof(s2), 1, fp_record);
printf("\n\nRoll :%d", s2.roll);
printf("\n\nName :%s", s2.name);
printf("\n\nMarks :%f", s2.marks);
}
printf("\n\n");
fclose(fp_index);
fclose(fp_record);
}
void display()
{
FILE *fp;
int flag = 1;
stud s2;
fp = fopen("record.txt", "rb");
while (fread(&s2, sizeof(s2), 1, fp))
{
if ((s2.roll) != 0)
{
if (flag == 1)
printf("\n\nRoll No Name marks");
flag = 0;
printf("\n\n%d", s2.roll);
printf(" %s", s2.name);
printf(" %f", s2.marks);
}
}
if (flag == 1)
printf("\n\nNo record found !!!");
printf("\n\n");
fclose(fp);
}
void delet(stud *s2, int r_n)
{
FILE *temp_fp, *fp;
fp = fopen("g.txt", "r+b");
temp_fp = fopen("temp.txt", "r+b");
while (fread(s2, sizeof(*s2), 1, fp))
{
if ((s2->roll) != r_n)
{
fseek(temp_fp, sizeof(*s2) * ((s2->roll) - 1), 0);
fwrite(s2, sizeof(*s2), 1, temp_fp);
}
}
fclose(fp);
unlink("g.txt");
rename("temp.txt", "g.txt");
fclose(temp_fp);
printf("\n\n");
}
void main()
{
FILE *fp;
stud s1, s2;
int choice, roll_no;
clrscr();
fp = fopen("record.txt", "a+b");
fclose(fp);
fp = fopen("temp.txt", "a+b");
fclose(fp);
fp = fopen("index.txt", "a+b");
fclose(fp);
while (1)
{
printf("\n\n1:Add 2:Display 3:Search 4:Edit 5:Delete 6:Exit \n\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
add();
break;
case 2:
display();
break;
case 3:
printf("\n\nEnter Roll No To Search Record : ");
scanf("%d", &roll_no);
search(roll_no);
break;
case 4:
printf("\n\nEnter Roll No To Edit Record : ");
scanf("%d", &roll_no);
edit(roll_no);
break;
case 5:
printf("\n\nEnter Roll No To Delete Record : ");
scanf("%d", &roll_no);
delet(&s2, roll_no);
break;
case 6:
exit(0);
default:
printf("\n\nInvalid choice !!!");
}
}
getch();
}

Screen Shots:

C_program_File_Student_Record

C_program_File_Student_Record_Output