c - I can't display binary content -


i'm trying write simple phone book program. have completed first function , according observe works without error. however, in second function (which ""display()"") can't show user after enter person knowledge. i'm working binary mode. problem in second function couldn't understand. if examine , i'll satisfied. in advance.

#include <stdio.h> #include <stdlib.h>      // "stdlib" library contains of exit() function #include <malloc.h>     // "malloc" library contains of malloc() function #include <windows.h>   // "windows" library contains of sleep() function waits system want #include <io.h>       // "io" library contains of filelength() function  struct personknowledge {     char name[32];     char surname[32];     char number[32]; };  file *ptrfile,*ptrfile1; long int recordlength,totalrecordlength,location; static int counter = 0; int number,totalrecordnumber;  void newrecord(); void display(); void delete(); void add(); void update();  int main() {     int choice;         {         printf("\n\t\t --- phone book program ---");         printf("\n\n\t\t 1) new record");   // options being presented user         printf("\n\n\t\t 2) display person knowledge");         printf("\n\n\t\t 3) delete someone");         printf("\n\n\t\t 5) update person knowledge");         printf("\n\n\t\t 6) exit");         printf("\n\n\nenter choice: ");         scanf("%d", &choice);         switch (choice)         {         case 1:         {             newrecord();             break;         }         case 2:         {             display();             break;         }         case 3:         {             break;         }         case 4:         {             break;         }         case 5:         {             break;         }         case 6:         {             printf("\nworking has been completed.\n");             return 0;         }         default:         {             printf("\nwrong entry! program has been terminated.\n");             break;         }         }     } while (choice >= 1 && choice <= 6);     return 0; }  void newrecord() {     if ((ptrfile = fopen("phone book.dat", "wb")) == null)     {         printf("the file couldn't open\n");         exit(0);     }     system("cls");   // screen being cleaned     struct personknowledge *p;   // p means person     p = (struct personknowledge *)malloc(sizeof(struct personknowledge));   // memory being allocated     fflush(stdin);     recordlength = sizeof(p);   // size of p     printf("||  %d. person  ||\n", counter+1);     printf("\n\express person name: ");   // user entering person's knowledge , being saved in file     gets(p->name);     printf("express %s's surname: ", p->name);     gets(p->surname);     printf("express %s's number: ", p->name);     gets(p->number);     fwrite(&(*p), recordlength, 1, ptrfile);     printf("\nplease wait, information saving file..\n");     sleep(750);     printf("*-* saving operation has been completed succesfully. *-*\n");     free(p);     counter++;     fclose(ptrfile); }  void display() {     if ((ptrfile = fopen("phone book.dat", "rb")) == null)     {         printf("the file couldn't open\n");         exit(0);     }     system("cls");   // screen being cleaned     struct personknowledge *s;   // s means searching     s = (struct personknowledge *)malloc(sizeof(struct personknowledge));     fflush(stdin);     recordlength = sizeof(s);     totalrecordlength = filelength(fileno(ptrfile));     totalrecordnumber = totalrecordlength / recordlength;     printf("\n\nexpress person record number search: ");     scanf("%d", &number);     location = (number - 1)*recordlength;     fseek(ptrfile, location, seek_set);     fread(&(*s), recordlength, 1, ptrfile);     printf("\n*-* person knowledge search *-*\n");     sleep(750);     printf("name: %s\n", s->name);     printf("surname: %s\n", s->surname);     printf("number: %s\n", s->number);     free(s);     fclose(ptrfile); } 

recordlength = sizeof(p); 

is wrong, size of pointer 4 on 32 bit system , 8 on 64 bit syste.

you need

recordlength = sizeof(*p); 

or

sizeof(struct personknowledge); 

which gives size of structure pointed p.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -