Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Assignment-II Tips.

Similar presentations


Presentation on theme: "Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Assignment-II Tips."— Presentation transcript:

1 Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Assignment-II Tips

2 Special binary file operations TRU-COMP3710 Introduction 2 int fseek(FILE*, long, SEEK_SET or SEEK_CURRENT or SEEK_END); // move file position pointer int fwrite(void*, int memb_size, int no_memb, FILE*); int fread(void*, int memb_size, int no_memb, FILE*); int ftell ( FILE * stream );

3 Defining the structure TRU-COMP3710 Introduction 3 The record or the structure is defined as: struct member { int mem_id; char mem_name[25]; int salary; }; * We may use typedef to define the user defined datatype as well

4 To create the file and add records TRU-COMP3710 Introduction 4 void create() { fp=fopen("Binary.txt", "ab"); struct member m; int ans=1; while (ans==1) { printf("\nEnter Member ID : "); scanf("%d", &m.mem_id); printf("\nEnter Member Name : "); scanf("%s",m.mem_name); printf("\nEnter Salary : "); scanf("%d", &m.salary); fwrite(&m, sizeof(struct member),1,fp); // this is writing the address of the structure on the file printf("\n\nPress '1' to add more and '0' to go back : "); scanf("%d", &ans); if (ans==0) {fclose(fp); return;} if (ans!=0 && ans !=1) printf("Invalid option entered "); } fclose(fp); }

5 Reading all the records TRU-COMP3710 Introduction 5 void display() { fp=fopen("Binary.txt", "rb"); struct member mem; int a; while(1) { fread(&mem, sizeof(struct member), 1, fp); // read a specific record at the record pointer if (feof(fp)) break; printf("\nMemebr ID : %d\nMember Name : %s\nSalary : %d\n", mem.mem_id, mem.mem_name, mem.salary); } printf("Press '0' to go back "); scanf("%d", &a); fclose(fp); }

6 Reading a specific record TRU-COMP3710 Introduction 6 In order to read a specific record, the number of record is taken from the user and then fseek is used to move the record pointer void search() { int a; struct member mem; fp=fopen("Binary.txt", "rb"); while(1) { printf("Enter the record number to search : "); scanf("%d", &a); fseek(fp, sizeof(struct member)*(a-1), 0); // seeking the location from the beginning of the file fread(&mem, sizeof(struct member),1, fp); printf("\nMemebr ID : %d\nMember Name : %s\nSalary : %d\n", mem.mem_id, mem.mem_name, mem.salary); printf("Press '1' to search more and '0' to go back : "); scanf("%d", &a); if (a==0) {fclose(fp); return;} if (a!=0 && a!=1) printf("Invalid input "); }

7 Fseek() TRU-COMP3710 Introduction 7 The fseek may be used to travel anywhere in the file: int fseek ( FILE * stream, long int offset, int origin ); The origin may be: Offset would be negative with SEEK_END

8 Rewind() TRU-COMP3710 Introduction 8 This positions the record pointer to the beginning of the file void rewind ( FILE * stream );

9 Update a record Introduction 9 First the record is read and it may also be written back with the new values void update() { int a; fp=fopen("Binary.txt", "r+"); //this would open in read as well as write mode char name_temp[25]; int sal_temp; struct member mem; while(1) { printf("Enter the record number to search : "); scanf("%d", &a); fseek(fp, sizeof(struct member)*(a-1), 0); //this is searching the specific record fread(&mem, sizeof(struct member),1, fp); printf("\n\nEnter new name : "); scanf("%s", mem.mem_name); //inputting the new name and salary printf("\n\nEnter new salary : "); scanf("%d", &mem.salary); fseek(fp, -sizeof(struct member), SEEK_CUR); //we need to go back to the original position to write back fwrite(&mem,sizeof(struct member), 1, fp); printf("\n\n Record Updated "); } TRU-COMP2130

10 Display the record TRU-COMP3710 Introduction 10 After reading the record, the simple display is: printf("\nMemebr ID : %d\nMember Name : %s\nSalary : %d\n", mem.mem_id, mem.mem_name, mem.salary); If we wish to define a function to display the members values: showOne(&mem); It is passing the address and pointer would receive the value

11 Using pointer values TRU-COMP3710 Introduction 11 void showOne(struct member *ptr) { printf("\nMember ID : %d", ptr->mem_id); //we use -> in spite of. printf("\nMember Name : %s", ptr->mem_name); printf("\nSalary : %d\n\n", ptr->salary); }

12 TRU-COMP2130 C: Advanced Topics 12 void print_rcd(struct student_rcd rcd) { printf(“Number: %d\n”, rcd.student_number); printf(“Name: %s\n”, rcd.name); // name is an array. // not &(rcd.name) } void read_rcd(struct student_rcd *rcd) { printf(“Enter number: “); scanf(“%d”, &(rcd->student_number)); // reference rqd printf(“Enter name: “); scanf(“%s”, rcd->name); // name is an array. // not &(rcd->name) }

13 Delete a record TRU-COMP3710 Introduction 13 Once created, we may edit the data, but cannot delete Read all records and transfer to other file except the one’s you wish to delete Rename the new file while (1) { fread(&mem,sizeof(struct member),1,fp); //reading from fp if (feof(fp)) break; if (strcmp(naam, mem.mem_name) == 0) { printf("A record with requested name found and deleted.\n\n"); found=1; } else { fwrite(&mem, sizeof(struct member), 1, fp_tmp); //writing on fp_tmp } remove("Binary.txt"); rename("tmp.bin", "Binary.txt");

14 Some extra methods: TRU-COMP3710 Introduction 14 void menu() //to show all the menu options { system("clear"); printf("\n\n\t\tM A I N M E N U \n"); printf("\t1. Create the Binary File \n"); printf("\t2. Display the contents of the file \n"); printf("\t3. Seek a specific record \n"); printf("\t4. Update the contents of a record \n"); printf("\t5. Delete a record for the specific name \n"); printf("\t6. Exit \n"); } int input() //to take user input {int a; printf("\n\t\tPlease Enter your choice.... "); scanf("%d", &a); return(a); }

15 Screen shots TRU-COMP3710 Introduction 15

16 Generating binary conversion Introduction 16 There may be two aspects: a. a void function – printing is done in the function b. a function returning character pointer – function just converts the decimal number to its binary equivalent in a character array and returns back the pointer of the array. TRU-COMP2130

17 Set target bit to 1 Introduction 17 This is an example of OR masking We need to generate a number with having 1 at the y place Mask x with y to get new x TRU-COMP2130

18 Get target byte Introduction 18 Initialize a number to 0 Make the target byte as 1 and other all bytes as 0, by left OR with 1 and left shift Make other bytes as 0 by only left shift 8 times Mask the generated number with the actual number to return only the target byte TRU-COMP2130


Download ppt "Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Assignment-II Tips."

Similar presentations


Ads by Google