C Programs using File Operations - Sequential file
Program for a sequential file organization, the input data is string of songs and the play function plays (actually displays) the desired song.
/* Program for a sequential file organization, the input data is string of songs and the play function plays the desired song */
#include <stdio.h>
#include <conio.h>
#include<stdlib.h> /*standard library function header file*/
#include <string.h> /*add(), play (), display() are the functions */
void add();
void play();
void display();
struct tape /*tape is a structure with integer sno and character string*/
{
int sno;
char song[20];
};
struct tape t;
FILE *fp = NULL; /* file_pointer */
void main()
{
int ch;
char ans = 'y';
while (ans == 'y' || ans == 'Y')
{
printf("MENU \n");
printf(" 1 ADD SONG \n");
printf(" 2 DISPLAY SONG \n");
printf(" 3 PLAY SONG \n");
printf(" 4 EXIT \n");
printf("Enter your choice :");
scanf("%d", &ch);
switch (ch)
{
case 1:
add(); /* function call */
break;
case 2:
display(); /* function call */
break;
case 3:
play(); /* function call */
break;
case 4:
exit(0);
break;
default:
exit(0);
} /* switch */
printf("\n \t continue to menu(y/n)...");
scanf("%c",&ans);
} /* while */
/* main */
void add()
{
char next = 'y';
fp=fopen("music.dat", "a"); /*fopen function creates a new file*/
if(fp== NULL)
{
printf("file open error ");
exit(0);
} /* if */
while (next == 'y' || next == 'Y')
{
printf("Enter song No :\t");
scanf("%d", &t. sno);
printf("Enter song name:\t");
gets (t.song);
fwrite(&t, sizeof(t),1,fp); /* fwrite function writes data to the file! */
printf("Add more songs (y/n)....");
scanf("%c", &next);
} /* while */
fclose(fp);
} /* add()* */
void display()
{
fp = fopen("music.dat", "r");
if(fp== NULL)
{
printf("file open error");
exit(0);
} /* if */
while (fread(&t, sizeof(t), 1,fp))
{
printf("\n \t");
printf("%d\t%s", &t.sno, t.song);
} /* while */
fclose(fp);
} /* display */
void play()
{
char name[20]; /* name is a character array */
int flag = 0;
printf("Enter song name to play :");
scanf("%s", name);
fp = fopen("music.dat", "r");
if(fp == NULL)
{
printf("file open error");
exit(0);
} /* if */
while (fread(&t, sizeof(t),1,fp))
{
if(strcmp(t.song, name)==0)
{
flag = 1;
break;
} /* if */
} /* while */
if(flag == 1)
{
printf("\n playing song……\n");
printf("song No.... %d\n", t.sno);
printf("song name.... %s", t.song);
} /* if */
else
printf("Not found");
fclose(fp); /* file close function closes a file*/
} /* play() */
OUTPUT
Menu
1 Add song
2 DISPLAY song
3 Play song
4 EXIT
Enter your choice :1
Enter song No: 1
Enter song name : o podu
Add more songs (y/n)....
continue to menu(y/n)...y
Menu
1 Add song
2. DISPLAY song
3 Play song
4 EXIT
Enter your choice : 1
Enter song No : 2
Enter song name: raja
Add more songs (y/n)....
continue to menu(y/n)...y
Menu
1 Add song
2 DISPLAY song
3 Play song
4 EXIT
Enter your choice choice :1
Enter song No : 3
Enter song name : kanne
Add more songs (y/n)....
continue to menu(y/n)...
Menu
1 Add song
2 DISPLAY song
3 Play song
4 EXIT
Enter your choice : 3
Enter song name to play : raja
playing song.....
song No.... 2
song name.... raja
continue to menu(y/n)... n
Computer Programming C Laboratory: File Operations C Programs : Tag: C Programming : - Program for a sequential file organization, the input data is string of songs and the play function plays (actually displays) the desired song
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation