-3

I am writing a phone book program. I completed first function.

However, in second function (("" display() function "")) there is something wrong which is I couldn't find.

In display() function, I'm taking another name which is searched by user and comparing it with names into file to show that person's knowledge (Just one person) on the screen. But it doesn't work. How can I solve this problem?

#include <stdio.h>
#include <stdlib.h>     // "stdlib" library contains of exit() and malloc function
#include <Windows.h>   // "Windows" library contains of Sleep() function which waits the system as you want
#include <string.h>   // "string" library contains of strcmp() function which compares string statements

struct personKnowledge
{
    char number[16];
    char name[16];
    char surname[16];
    char sName[16];

};

void newRecord(FILE *);
void display(FILE *);
void deletE();
void add();
void update();

FILE *ptrFILE;

int main()
{
    int choice;
    if ((ptrFILE = fopen("Phone Book.txt", "w+")) == NULL)
    {
        printf("The file couldn't open\n");
    }
    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 4) Add new person");
        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(ptrFILE);
            break;
        }
        case 2:
        {
            display(ptrFILE);
            break;
        }
        case 3:
        {
            break;
        }
        case 4:
        {
            break;
        }
        case 5:
        {
            break;
        }
        case 6:
        {
            printf("\nWorking has been completed.\n");
            exit(EXIT_SUCCESS);
            break;
        }
        default:
        {
            printf("\nWrong entry! The program has been terminated.\n");
        }
        }
    } while (choice >= 1 && choice <= 6);
    fclose(ptrFILE);
    return 0;
}

void newRecord(FILE *ptrFILE)
{
    static int counter = 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);
    printf("\n\nDetermine person name: ");   // User is entering the person's knowledge and they are being saved in file
    gets(p->name);
    printf("Determine %s's surname: ", p->name);
    gets(p->surname);
    printf("Determine %s's number: ", p->name);
    gets(p->number);
    if (counter == 0)
    {
        fprintf(ptrFILE, "Name\t\t\t\tSurname\t\t\t\tNumber\n");
        fprintf(ptrFILE, "--------\t\t   ----------------\t\t------------------------\n");
    }   
    fprintf(ptrFILE, "\n%-33s%-33s%-38s\n", p->name, p->surname, p->number);
    printf("Please wait, information is saving to file..\n");
    Sleep(750);
    printf("*-* Saving operation has been completed. *-*\n");
    counter++;
    free(p);
}

void display(FILE *ptrFILE)
{
    if ((ptrFILE = fopen("Phone Book.txt", "r")) == NULL)
    {
        printf("The file couldn't open\n");
    }
    else
    {
        system("cls");   // Screen is being cleaned
        struct personKnowledge *s;   // s means searching
        s = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));
        fseek(ptrFILE, 0L, SEEK_SET);
        fflush(stdin);
        printf("\n\nExpress name which you search: ");
        gets(s->sName);
        while (!feof(ptrFILE))
        {
            fscanf(ptrFILE, "\n%-33s%-33s%-38s\n", &s->name, &s->surname, &s->number);
            if (strcmp(s->name, s->sName) == 0)
            {
            printf("*-* Person knowledge who is you search *-*\n");
            Sleep(750);
            printf("\n\nName:  %s\nSurname:  %s\nNumber:  %s\n", s->name, s->surname, s->number);
            }
        }
        free(s);
    }   
}
NoWeDoR
  • 49
  • 5

3 Answers3

1

The MSVC documentation of fopen says about mode: "w" Opens an empty file for both reading and writing. If the file exists, its contents are destroyed.

Since the first file open in main is

if ((ptrFILE = fopen("Phone Book.txt", "w+")) == NULL)

you destroy anything you already have.

To read the content, use mode "r" to open the file, read the content, then close it.

To add new content, either re-open with mode "w" and write the whole content, or open in append mode "a" and just write the new records(s).

Or you can open in mode "r+" for reading and writing, but before writing you need to fseek the end of the file.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1

In int main() you opened file with "w+" mode so everything is discarded in file when it is called.

Also in function void display(FILE *ptrFILE) you have not closed the text file.And you have used feof() inside while loop which may create problem .

Please see following link why you should not use while(!feof())-Why is “while ( !feof (file) )” always wrong?

Community
  • 1
  • 1
ameyCU
  • 16,489
  • 2
  • 26
  • 41
0
void display(FILE *ptrFILE)
{
    fclose(ptrFILE);//!! flush out
    if ((ptrFILE = fopen("Phone Book.txt", "r")) == NULL)
    {
        printf("The file couldn't open\n");
    }
    else
    {
        char buff[128];//!!for fgets
        system("cls");   // Screen is being cleaned
        struct personKnowledge *s;   // s means searching
        s = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));
        //fseek(ptrFILE, 0L, SEEK_SET);//!!no need
        fflush(stdin);
        printf("\n\nExpress name which you search: ");
        gets(s->sName);
        while (fgets(buff, sizeof buff, ptrFILE))//!!
        {
            sscanf(buff, "%15s%15s%15s\n", s->name, s->surname, s->number);//!!
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • I don't get where you used `fclose(ptrFILE)` in start of the function .What will it do in reference to fopen in `main()`.? – ameyCU Jul 06 '15 at 16:44
  • `fclose(ptrFILE)` for next `fopen(.., "r")...`. How it may be in if there is integrity of the file. – BLUEPIXY Jul 06 '15 at 16:48
  • I carried fclose(ptrFILE) to newRecord() function's end :) – NoWeDoR Jul 06 '15 at 16:49
  • @BLUEPIXY ok I am asking straight - why you used fclose() in start ? I really don't get it . – ameyCU Jul 06 '15 at 16:51
  • But there is something wrong here, too. For example if you enter double name and try to show it just show once. and once of that names will be surname :((( – NoWeDoR Jul 06 '15 at 16:52
  • @NoWeDoR When in order to perform the command separately to write a new record , use `fopen (..., "a");` – BLUEPIXY Jul 06 '15 at 16:52
  • I think I can also rewrite the entire, But I think it is your job. – BLUEPIXY Jul 06 '15 at 16:59