| | |
find the cause of the error in the C prog
Thread Solved
![]() |
•
•
Join Date: Jul 2006
Posts: 4
Reputation:
Solved Threads: 0
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:
#[PHP] 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);
}[/PHP]Please suggest the cause of this error!
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:
#[PHP] 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);
}[/PHP]Please suggest the cause of this error!
![]() |
Similar Threads
- Visual C++ error LNK2022: metadata operation failed (80131187) (C++)
- I got a Link error 1181 with fresh code from source safe. (C++)
- Window Xp IE 6 (DNS ERROR) page not found! (Viruses, Spyware and other Nasties)
- unknown error message keeps appearing!! (Windows NT / 2000 / XP)
- Another "cannot find symbol" compiling error (Java)
- "Connection was refused error". Problems signing into Hotmail & Yahoo (secure) (Web Browsers)
- Error message on start up - bridge.dll file missing (Viruses, Spyware and other Nasties)
- RUNDLL Error Message at Boot (Viruses, Spyware and other Nasties)
- cannot find server/page or DNS error (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: problem with the library
- Next Thread: Hard to understand "Inheritance"
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





