Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#define DATABASENAME \"database.bin\" int main() { if (loadDatabase(DATABASENAME

ID: 2246919 • Letter: #

Question

#define DATABASENAME "database.bin"

int main() {

if (loadDatabase(DATABASENAME)) {

struct _record tmp;

if (getRecord(DATABASENAME, 0, &tmp))

showRecord(&tmp);

if (getRecord(DATABASENAME, 3, &tmp))

showRecord(&tmp);

if (getRecord(DATABASENAME, 5, &tmp))

showRecord(&tmp);

if (changeRecord(DATABASENAME, 4, "lname", "Shaffer"))

if (changeRecord(DATABASENAME, 4, "phone1", "814-555-1212"))

if (getRecord(DATABASENAME, 4, &tmp))

showRecord(&tmp);

}

return 0;

}

Use a struct to store the data. Only process one struct of data (a record) at a time.

Make sure you implement random access (if you don't have fseek you're probably not doing it right).

Make sure you open and manipulate your files in binary mode.

Every function shown in main needs to open and close the file within itself (don't leave it open throughout).

Explanation / Answer

#include<stdio.h>
#include<stdbool.h>
#define DATABASENAME "data.bin"
/* Structure to hold data of records*/
struct data
{
   int rec;
};
// Check if connection to binary file is successful
bool loadDatabase(){
FILE *ptr_myfile;
bool flag = true;
ptr_myfile=fopen(DATABASENAME,"wb");
if (!ptr_myfile)
{
printf("Unable to open file!");
flag = false;
}
fclose(ptr_myfile);
return flag;
}
// Read record in binary file
bool getRecord(int position, struct data *record){
FILE *ptr_myfile;
bool flag = true;
ptr_myfile=fopen(DATABASENAME,"rb");
// Seek the location of the object depending upon the requested position
fseek(ptr_myfile,sizeof(struct data)*position,SEEK_SET);
   // Read one record of data size into record variable
   if(fread(&record,sizeof(struct data),1,ptr_myfile)!= 1){ // If one element is successfully read
   flag=false;
   }
   printf("%d",position);
fclose(ptr_myfile);
return flag;
}
// Display record to console
void showRecord(struct data *record){
printf("%d ",record->rec);
}
// Update record in binary file
bool changeRecord(int position, int value){
FILE *ptr_myfile;
bool flag = true;
struct data record;
// Open file in write mode
ptr_myfile=fopen(DATABASENAME,"wb");
  
// Seek the location of the object depending upon the requested position
fseek(ptr_myfile,sizeof(struct data)*position,SEEK_SET);
   record.rec= value;
   // Read one record of data size into record variable
   if(fwrite(&record, sizeof(struct data), 1, ptr_myfile)!= 1){ // If one element is successfully updated
   flag=false;
   }
  
fclose(ptr_myfile);
return flag;
}

// Main method
int main()
{
if (loadDatabase()) {
struct data tmp;
  
if (getRecord(0, &tmp) == true){
showRecord(&tmp);
  
}
  
if (getRecord(3, &tmp) == true)
showRecord(&tmp);
  
if (getRecord(5, &tmp) == true)
showRecord(&tmp);

if (changeRecord(4, 1000) == true)
if (changeRecord(4, 999) == true)
if (getRecord(4, &tmp) == true)
showRecord(&tmp);
}
return 0;
}