Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions prog1/prog1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <stdio.h>

typedef struct{
int SRN;
char name[50];
float marks;
}Student;

void storeRecords(int n);
void mthRecord(int m);
void deleteRecord(int SRN);

int main() {
int n, m, SRN;

printf("Enter number of records:\n");
scanf("%d", &n);

storeRecords(n);

printf("\nEnter mth record to display:\n");
scanf("%d", &m);

mthRecord(m);

printf("\nEnter SRN to delete:");
scanf("%d", &SRN);

deleteRecord(SRN);
return 0;
}

void storeRecords(int n){
FILE* fp;
Student s;
fp = fopen("recoders.bin", "wb");

if(fp == NULL){
printf("Error opening file\n");
return;
}

for(int i = 0; i<n; i++){
printf("Students Record:%d\n", i+1);

printf("SRN: ");
scanf("%d", &s.SRN);

printf("Name: ");
scanf("%s", s.name);

printf("Marks: ");
scanf("%f", &s.marks);

fwrite(&s, sizeof(s), 1, fp);
}

fclose(fp);

printf("Records Stored successfully\n");

}

void mthRecord(int m){
FILE *fp;
Student s;

fp = fopen("recoders.bin", "rb");

if(fp == NULL){
printf("Error opening file\n");
return;
}

fseek(fp, (m-1)*sizeof(Student), SEEK_SET);

if(fread(&s, sizeof(Student), 1, fp) == 1){
printf("SRN: %d\n", s.SRN);
printf("Name: %s\n", s.name);
printf("Marks: %.2f", s.marks);
}else{
printf("Record not found");
}

fclose(fp);

}


void deleteRecord(int SRN){
FILE *fp, *temp;
Student s;

int found = 0;

fp = fopen("recoders.bin", "rb");
temp = fopen("temp.bin", "wb");
if(fp == NULL || temp == NULL){
printf("Error opening file\n");
return;
}

while(fread(&s, sizeof(Student), 1, fp)){
if(s.SRN == SRN){
found = 1;
continue;
}
fwrite(&s, sizeof(Student), 1, temp);
}

fclose(fp);
fclose(temp);

remove("records.bin");
rename("temp.bin", "records.bin");

if(found){
printf("Record deleted successfully\n");
}else{
printf("Record not found\n");
}
}

Binary file added prog1/prog1.exe
Binary file not shown.
Empty file added prog1/recoders.bin
Empty file.
Binary file added prog1/records.bin
Binary file not shown.
117 changes: 117 additions & 0 deletions prog2/prog2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <stdio.h>

typedef struct{
int SRN;
char name[50];
float marks;
}Student;

void records(Student s[], int n);
int createseekpositions(int pos[], int maxrecords);
void displayrecords(int position);

int main() {
int n, choice;
int positions[100];

printf("Enter number of records:");
scanf("%d", &n);

Student s[n];

for(int i=0; i<n; i++){
printf("Enter details of the student: %d\n", i+1);

printf("Enter SRN:");
scanf("%d", &s[i].SRN);

printf("Enter Name:");
scanf("%s", s[i].name);

printf("Enter marks:");
scanf("%f", &s[i].marks);
}

records(s, n);
int total = createseekpositions(positions, 100);

printf("\nSeek positions:\n");

for(int i=0; i<n; i++){
printf("Record %d starts at position %d\n", i+1, positions[i]);
}

printf("\nEnter record number to display: ");
scanf("%d", &choice);

if(choice >= 1 && choice <= total){
displayrecords(positions[choice - 1]);
}else{
printf("\nInvalid record number");
}
return 0;
}

void records(Student s[], int n){
FILE *fp;
fp = fopen("records.txt", "w");

if(fp == NULL){
printf("Error opening file\n");
return;
}

for(int i=0; i<n; i++){
fprintf(fp, "%d %s %.2f\n", s[i].SRN, s[i].name, s[i].marks);
}

fclose(fp);
printf("Records stored successfully");
}

int createseekpositions(int pos[], int maxrecords){
FILE *fp;
int count = 0;
Student temp;

fp = fopen("records.txt", "r");

if(fp == NULL){
printf("Error opening file\n");
return 0;
}

while(!feof(fp) && count < maxrecords){
pos[count] = ftell(fp);
if(fscanf(fp, "%d %S %f", &temp.SRN, temp.name, &temp.marks) == 3){
count++;
}

}
fclose(fp);
return count;
}

void displayrecords(int position){
FILE *fp;
Student s;
fp = fopen("records.txt", "r");

if(fp == NULL){
printf("Error opening file\n");
return;
}

fseek(fp, position, SEEK_SET);

if(fscanf(fp, "%d %s %f", &s.SRN, s.name, &s.marks) == 3){
printf("SRN: %d\n", s.SRN);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
}else{
printf("Record not found\n");
}

fclose(fp);
}

Binary file added prog2/prog2.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions prog2/records.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 a 30.00
2 b 60.00
3 c 90.00
41 changes: 41 additions & 0 deletions prog3/prog3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>

void printbits(int num);
int countsetbits(int num);


int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);

printf("\nBinary Representation: ");
printbits(n);

printf("\nNumber of 1 bits = %d\n", countsetbits(n));

return 0;
}

void printbits(int num){
int totalbits = sizeof(int) * 8;
for(int i=totalbits -1; i>=0; i--){
int bit = (num >> i)&1;
printf("%d", bit);

}
printf("\n");
}

int countsetbits(int num){
int count = 0;
while(num!=0){
if((num&1)==1)
count++;

num = num>>1;
}


return count;
}
Binary file added prog3/prog3.exe
Binary file not shown.
25 changes: 25 additions & 0 deletions prog4/prog4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>

struct Bitfield{
unsigned int firstbit:1;
unsigned int secondbit:2;
unsigned int thirdbit:3;
};

int main() {
struct Bitfield a;

a.firstbit = 1;
a.secondbit = 2;
a.thirdbit = 3;

printf("firstbit:%u\n", a.firstbit);
printf("secondbit:%u\n", a.secondbit);
printf("thirdbit:%u\n", a.thirdbit);

a.firstbit = 3;

printf("\nAfter Assigning 3 to firstbit: \n");
printf("firstbit = %u\n", a.firstbit);
return 0;
}
Binary file added prog4/prog4.exe
Binary file not shown.
Loading