Programlama dersini alan tüm öğrencilere ait bilgiler aşağıda verilen struct ile rastgele erişimli bir dosyada tutulmaktadır. Dosya içerisinde bulunan iki kaydın bilgilerinin yer değiştirilmesi istenmektedir. Bu işlemi gerçekleştiren kodu yazınız. Note: The position of the 2 records that you need to swap like this: The position of the first record is the sum of the first 5 digits of your student number. The position of the second record is the sum of the last 5 digits of your student number. Örnek (Examples): if your student number is : 1910202034 : then you need to swap recods 13 <--> 9 if your student number is: 2014010217021 : then you need to swap recods 7 <--> 11 Dosya (File): struct student { 1.record int number; 2.record float midterm: 3.record char name[40]: n.record
/Main.c//////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//_______
struct student{
int number;
float midterm;
char name[40];
};
//_______
//_______
int swapRecord(FILE *filePtr, int first, int last)
{
fseek(filePtr, 0, SEEK_SET);
int count=1;
struct student *stud,*stud1=NULL,*stud2=NULL;
while (!feof(filePtr)){
stud=(struct student*)malloc(sizeof(struct student));
int result = fread(stud, sizeof(struct student), 1, filePtr);
if (result != 0 && count==first){
stud1=stud;
}
else if(result != 0 && count==last){
stud2=stud;
}
count++;
}
if(stud1==NULL || stud2==NULL)
return 0;
fseek(filePtr, 0, SEEK_SET);
count=1;
while (!feof(filePtr)){
fread(stud, sizeof(struct student), 1, filePtr);
if(count==first){
fseek(filePtr, -(long)sizeof(struct student), 1);
fwrite(stud2, sizeof(struct student), 1, filePtr);
if(first>last)
return 1;
}
if(count==last){
fseek(filePtr, -(long)sizeof(struct student), 1);
fwrite(stud1, sizeof(struct student), 1, filePtr);
if(last>first)
return 1;
}
count++;
//fseek(filePtr, (long)sizeof(struct student), 1);
}
return 1;
}
//_________
void addStudent(FILE *filePtr, int number, float midterm, char name[40]){
struct student student;
student.number=number;
strcpy(student.name,name);
student.midterm=midterm;
fwrite(&student, sizeof(struct student), 1, filePtr);
}
//_______
void showRecords(FILE *filePtr){
fseek(filePtr, 0, SEEK_SET);
printf("\n%-6s %-8s %-20s\n","Number","Midterm","Name");
while (!feof(filePtr)){
struct student student;
int result = fread(&student, sizeof(struct student), 1, filePtr);
if (result != 0){
printf("%-6d %-8f %-20s\n",student.number,student.midterm,student.name);
}
}
}
//_______
int main(){
char roll[30];
printf("Enter roll number: ");
scanf("%s",roll);
int len=strlen(roll);
int last=0,first=0;
for(int i=0;i<5;i++){
first=first+roll[i]-48;
last=last+roll[len-1-i]-48;
}
printf("First record number: %d\n",first);
printf("Last record number: %d\n",last);
FILE *filePtr;
filePtr = fopen("input.txt","wb+");
if (filePtr == NULL){
printf("Could not open input.txt");
return 0;
}
addStudent(filePtr,54,67.8,"Alex");
addStudent(filePtr,67,90,"Bob");
addStudent(filePtr,23,69,"Dillon");
addStudent(filePtr,89,80,"Mojesh");
showRecords(filePtr);
if(swapRecord(filePtr,first,last)==0){
printf("Invalid record number!\n");
return 0;
}
printf("\nRecord number %d is swapped with record number %d\n",first,last);
showRecords(filePtr);
fclose(filePtr);
return 0;
}
//end of Main.c//////////////////////
Step by step
Solved in 2 steps with 1 images