I am trying to prepare a database using C,which can store data and from which data can be retrieved and edited.
First storing,then retrieving data from the file works.But,during first run of the prog if choice#2(i.e.,DISPLAY)is entered without entering any data using choice#1(although the file contains data),then error occurs,gets hung.A part of the prog is shown:

#

include<stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>typedef struct {
  char name[30];
  char company[50];
  char address[200];
  char pin[7];
  char web[30];
  }entry;
FILE *fpt;int menu(void);
entry *ptr,commodity;
int n=sizeof(entry);
void main()
{
 int choice;
 do
 {
 choice=menu();
 switch(choice)
 {
  case 1:ptr=(entry *)malloc(sizeof(entry));
   fpt=fopen("data4.bin","a");
   printf("\nEnter the name of the commodity:");
   scanf("%s",ptr->name);
   printf("\nEnter the name of the producing company:");
   scanf("%s",ptr->company);
   printf("\nEnter the address of the producing company:");
   scanf("%s",ptr->address);
   printf("\nEnter the pincode:");
   scanf("%s",ptr->pin);
   printf("\nEnter the website of the producing company:");
   scanf("%s",ptr->web);
   fwrite(ptr,n,1,fpt);printf("ENTERED\n");
   fclose(fpt);
   continue;
  case 2:fpt=fopen("data4.bin","r");
         
     do 
     {
     if (fread(&ptr->name, sizeof(ptr->name), 1, fpt) == 1 &&
       fread(&ptr->company, sizeof(ptr->company), 1, fpt) == 1 &&
       fread(&ptr->address, sizeof(ptr->address), 1, fpt) == 1 &&
       fread(&ptr->pin, sizeof(ptr->pin), 1, fpt) == 1 && 
       fread(&ptr->web, sizeof(ptr->web), 1, fpt) == 1)
         {
         printf("\n\nThe name of the commodity:");
      printf("%s",ptr->name);
      printf("\nThe name of the producing company:");
      printf("%s",ptr->company);
      printf("\nThe address of the producing company:");
      printf("%s",ptr->address);
      printf("\nPincode:");
      printf("%s",ptr->pin);
      printf("\nWebsite of the producing company:");
      printf("%s",ptr->web);
        }
   else if (feof(fpt)) 
          {
    printf("\n\nfinished reading file!\n");
    break;
    }
   else {
    printf("an error occured while reading the file!\n");
    break;
    }
} while (1);
         fclose(fpt);continue;
    case 3:printf("\nThank You for using!");exit(1);              
 }
 }while(choice==1||choice==2);
 getch();
}
int menu(void)
{
 int choice;
 do{
  printf("Enter your choice:\n");
  printf("1.CREATE the linked list\n");
  printf("2.DISPLAY\n");
  printf("3.EXIT\n");
  scanf("%d",&choice);
 }while(choice!=1 && choice!=2 && choice!=3);
 return(choice);
}

Please suggest the cause of this error!

Dani AI

Generated

spotted the immediate cause: the DISPLAY path reads into memory through ptr before ptr points at valid storage. That yields undefined behavior (crashes, hangs, or silent memory corruption). The fix is to ensure reads write into valid storage (either allocate ptr or read into a local entry variable) and to read the file the same way it was written.

Practical, safer approach: read whole records with a single fread into a stack variable and print that record. This avoids juggling many small fread calls and reduces chances of mismatched sizes or pointer mistakes.

entry rec;
FILE *f = fopen("data4.bin", "rb");
if (f) {
  while (fread(&rec, sizeof rec, 1, f) == 1) {
    printf("%s\n%s\n%s\n%s\n%s\n", rec.name, rec.company, rec.address, rec.pin, rec.web);
  }
  fclose(f);
}

Other important improvements and checks:

  • Always check fopen, malloc, fread, and fwrite return values and handle errors (use perror to get diagnostics).
  • Use binary modes ("rb", "ab") when storing raw structs to avoid platform-specific translation.
  • Avoid scanf("%s", ...) for string input: it stops at whitespace and risks buffer overflow. Use fgets with buffer limits and trim the trailing newline.
  • Prefer int main(void) over void main() for standards compliance.
  • If dynamic allocation is used, check for NULL and free when done.

For reference on the I/O and allocation functions and their return-value behavior see the C documentation: fread/fwrite - cppreference and . These practices make the program robust and prevent the undefined behavior that caused the hang.

Recommended Answers

All 3 Replies

During the first choice how will the file contains data????
The file will not be created at all ...

Got the problem. In case 2 u have to declare ptr.
Use ptr=(entry *)malloc(sizeof(entry)); after case 2 also

Thanks a lot Mr.dilip.mathews,your suggestion helped me to correct my prog.Now it is working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.