我无法显示二进制内容

时间:2015-07-13 12:39:23

标签: c

我正在尝试编写一个简单的电话簿程序。我已经完成了第一个功能,据我观察它没有错误。但是,在第二个功能(即"" display()"")中,在输入人员知识后,我无法向用户显示。我正在使用二进制模式。第二个功能的问题是我无法理解的。如果你检查并帮助我,我会感到满意。提前谢谢。

#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 which waits the system as you 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;
    do
    {
        printf("\n\t\t --- Phone Book Program ---");
        printf("\n\n\t\t 1) New record");   // The options are being presented to 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 your 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! The 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 is being cleaned
    struct personKnowledge *p;   // p means person
    p = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));   // Memory is being allocated
    fflush(stdin);
    recordLength = sizeof(p);   // size of p
    printf("||  For the %d. person  ||\n", counter+1);
    printf("\n\Express person name: ");   // User is entering the person's knowledge and they are 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 is saving to 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 is 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 which you search: ");
    scanf("%d", &number);
    location = (number - 1)*recordLength;
    fseek(ptrFILE, location, SEEK_SET);
    fread(&(*s), recordLength, 1, ptrFILE);
    printf("\n*-* Person knowledge which you 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);
}

1 个答案:

答案 0 :(得分:2)

recordLength = sizeof(p);

是错误的,这是指针的大小,在32位系统上通常为4,在64位系统上为8。

你需要

recordLength = sizeof(*p);

sizeof(struct personKnowledge);

,它为您提供p指向的结构的大小。