Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,152,144 members, 7,815,012 topics. Date: Thursday, 02 May 2024 at 04:43 AM |
Nairaland Forum / Science/Technology / Programming / I Am New To C Programming, I Am Having Issue With This Code (1211 Views)
I Gave Up On Programming. I'm Tired Of Programming. I Need To Rest. / I Feel Like Giving Up In Learning Programming. I Find It Hard!! / C Programming Assignment Help ? (2) (3) (4)
(1) (Reply)
I Am New To C Programming, I Am Having Issue With This Code by idogbo(m): 7:41am On Nov 26, 2017 |
#include<stdio.h> struct student { int number; char name, surname[30]; }stud; // FUNCTION TO INSERT RECORDS TO PHONEBOOK void insert() { FILE *fp; fp = fopen("Record", "a" printf("Enter Number :" scanf("%d", &stud.number); printf("Enter Name :" scanf("%s", &stud.name); printf("Enter Surname :" scanf("%s", &stud.surname); fwrite(&stud.number, sizeof(stud.number), 1, fp); fwrite(&stud.name, sizeof(stud.name), 1, fp); fwrite(&stud.surname, sizeof(stud.surname), 1, fp); fclose(fp); } // FUNCTION TO DISPLAY RECORDS void disp() { FILE *fp1; fp1 = fopen("Record", "r" printf("\nNumber\tName\tSurname\n\n" while (fread(&stud.number, sizeof(stud.number), 1, fp1) printf(" %d\t\t", stud.number); (fread(&stud.name, sizeof(stud.name), 1, fp1) printf(" %s\t", stud.name, stud.surname); (fread(&stud.surname, sizeof(stud.surname), 1, fp1 printf(" %s\n", stud.surname); fclose(fp1); } // FUNCTION TO SEARCH THE GIVEN RECORD void search() { FILE *fp2; int r, s, avl; printf("\nEnter the Number you want to search :" scanf("%d", &r); avl = avlnumber(r); if (avl == 0) printf("Number %d is not available in the file\n",r); else { fp2 = fopen("Record", "r" while (fread(&stud, sizeof(stud), 1, fp2)) { s = stud.number; if (s == r) { printf("\nMobile no = %d", stud.number); printf("\nName = %s", stud.name); printf("\nSurname = %s\n", stud.surname); } } fclose(fp2); } } // FUNCTION TO DELETE A RECORD void deletefile() { FILE *fpo; FILE *fpt; int r, s; printf("Enter the Phone number you want to delete :" scanf("%d", &r); if (avlnumber(r) == 0) printf("Phone number %d is not available in the file\n", r); else { fpo = fopen("Record", "r" fpt = fopen("TempFile", "w" while (fread(&stud, sizeof(stud), 1, fpo)) { s = stud.number; if (s != r) fwrite(&stud, sizeof(stud), 1, fpt); } fclose(fpo); fclose(fpt); fpo = fopen("Record", "w" fpt = fopen("TempFile", "r" while (fread(&stud, sizeof(stud), 1, fpt)) fwrite(&stud, sizeof(stud), 1, fpo); printf("\nRECORD DELETED\n" fclose(fpo); fclose(fpt); } } // FUNCTION TO UPDATE THE RECORD void update() { int avl; FILE *fpt; FILE *fpo; int s, r, ch; printf("Enter Phone number to update:" scanf("%d", &r); avl = avlnumber(r); if (avl == 0) { printf("phone number %d is not Available in the file", r); } else { fpo = fopen("Record", "r" fpt = fopen("TempFile", "w" while (fread(&stud, sizeof(stud), 1, fpo)) { s = stud.number; if (s != r) fwrite(&stud, sizeof(stud), 1, fpt); else { printf("\n\t1. Update Phone Number %d", r); printf("\n\t2. Update Name %s", r); printf("\n\t3. Update Surname %s", r); printf("\nEnter your choice:" scanf("%d", &ch); switch (ch) { case 1: printf("Enter Number:" scanf("%d", &stud.number); break; case 2: printf("Enter Name : " scanf("%s", &stud.name); break; case 3: printf("Enter Surname: " scanf("%s", &stud.surname); break; default: printf("Invalid Selection" break; } fwrite(&stud, sizeof(stud), 1, fpt); } } fclose(fpo); fclose(fpt); fpo = fopen("Record", "w" fpt = fopen("TempFile", "r" while (fread(&stud, sizeof(stud), 1, fpt)) { fwrite(&stud, sizeof(stud), 1, fpo); } fclose(fpo); fclose(fpt); printf("RECORD UPDATED" } } /* FUNCTION TO SORT THE RECORD */ void sort() { int a[20], count = 0, i, j, t, c; FILE *fpo; fpo = fopen("Record", "r" while (fread(&stud, sizeof(stud), 1, fpo)) { a[count] = stud.number; count++; } c = count; for (i = 0; i<count - 1; i++) { for (j = i + 1; j<count; j++) { if (a[i]>a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } } printf("Phone No.\tName\t\tSurname\n\n" count = c; for (i = 0; i<count; i++) { rewind(fpo); while (fread(&stud, sizeof(stud), 1, fpo)) { if (a[i] == stud.number) printf("\n %d\t\t %s \t\t %s",stud.number, stud.name, stud.surname); } } } // FUNCTION TO CHECK GIVEN ROLL NO IS AVAILABLE // int avlnumber(int rno) { FILE *fp; int c = 0; fp = fopen("Record", "r" while (!feof(fp)) { fread(&stud, sizeof(stud), 1, fp); if (rno == stud.number) { fclose(fp); return 1; } } fclose(fp); return 0; } //FUNCTION TO CHECK THE FILE IS EMPTY OR NOT int empty() { int c = 0; FILE *fp; fp = fopen("Record", "r" while (fread(&stud, sizeof(stud), 1, fp)) c = 1; fclose(fp); return c; } // MAIN PROGRAM void main() { int c, emp; do { printf("---------------DELIMITER---------------\n" printf("\n\t1. Add new record to phone book\n\t2. Display Phonebook record\n\t3. Search phonebook" printf("\n\t4. Delete Contact\n\t5. Update Phonebook\n\t6. Sort phonebook" printf("\n\t7. EXIT" printf("\n\---------------DELIMITER---------------\n" printf("\nChoose your Operation:" scanf("%d", &c); printf("\n" switch (c) { case 1: insert(); break; case 2: emp = empty(); if (emp == 0) printf("\nThe file is EMPTY\n" else disp(); break; case 3: search(); break; case 4: deletefile(); break; case 5: update(); break; case 6: emp = empty(); if (emp == 0) printf("\n The file is EMPTY\n" else sort(); break; case 7: exit(1); break; default: printf("\nYour choice is wrong\nPlease try again...\n" break; } } while (c != 7); |
Re: I Am New To C Programming, I Am Having Issue With This Code by jerflakes(m): 11:43am On Nov 26, 2017 |
Guy you for just learn c++ 6 Likes |
Re: I Am New To C Programming, I Am Having Issue With This Code by romme2u: 5:58pm On Dec 01, 2017 |
first your code formatting is bad. some of the functions you invoke requires stdlib.h and other header files you have not included some of your function definition requires function prototype declaration. i think you should follow the advice of the guy above me. C# is not a bad idea either as both of them initializes the environment for you. but if you still want to stick out with C then get a C manual and join stackoverflow cc stack1 1 Like |
(1) (Reply)
Do You Need Nigeria States And Local Governments? / How Can I Create Another Version Of A Website? / Dragon, A New Programming Language And Its Uses
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 22 |