A
Anks
i am unable to find why following code is giving segmentation
fault.... way to produce seg fault: run the program... give input
12345678....enter any key except 'x'.... again give 12345678 as
input...then segmentation fault happens...
please somebody enlighten me...
#include <stdio.h>
#include<stdlib.h>
typedef struct
{
char project_id[40];
int project_ID;
} project_detail;
int
get_valid_int (char input[])
{
int count = 0;
while (input[count] != '\0' && count < 9)
{
if (input[count] >= '0' && input[count] <= '9')
count++;
else
return -1;
}
if (count != 8)
return -1;
else
return (atoi (input));
}
int
validate_projectid (int project_id)
{
FILE *fptr;
project_detail *temp;
int found = 0;
fptr = fopen ("PROJECT_DETAILS.DB", "r");
if (fptr != NULL)
{
fread (temp, sizeof (project_detail), 1, fptr);
while ((found == 0) && (!feof (fptr)))
{
if (temp->project_ID == project_id)
found = -1;
fread (temp, sizeof (project_detail), 1, fptr);
}
fclose (fptr);
}
return ((found >= 0) ? 1 : -1);
}
void
generation_of_project_details_add ()
{
project_detail proj_details;
char choice;
int search = 0;
FILE *f_ptr;
/*taking all the value from the keyboard */
do
{
/* Checking validity of Project detail ID */
do
{
printf ("\n Give the identification number of project\n");
scanf ("%s", proj_details.project_id);
fflush (stdin);
proj_details.project_ID = get_valid_int
(proj_details.project_id);
if (proj_details.project_ID == -1)
{
printf ("Invalid number. Pls enter again.\n");
continue;
}
search = validate_projectid (proj_details.project_ID);
/* Give error message if input is invalid */
if (proj_details.project_ID == -1 || search == -1)
printf
("\n The given input is invalid... Please try again...
\n");
}
while (proj_details.project_ID == -1 || search == -1); /* end
while
loop(inner) */
f_ptr = fopen ("PROJECT_DETAILS.DB", "a");
fwrite (&proj_details, sizeof (proj_details), 1, f_ptr);
fclose (f_ptr);
printf
("Do you want to input any other project detail. Press any key
to continue and press x to exit");
scanf (" %c", &choice);
}
while (choice != 'x');
}
int
main ()
{
generation_of_project_details_add ();
return 0;
}
fault.... way to produce seg fault: run the program... give input
12345678....enter any key except 'x'.... again give 12345678 as
input...then segmentation fault happens...
please somebody enlighten me...
#include <stdio.h>
#include<stdlib.h>
typedef struct
{
char project_id[40];
int project_ID;
} project_detail;
int
get_valid_int (char input[])
{
int count = 0;
while (input[count] != '\0' && count < 9)
{
if (input[count] >= '0' && input[count] <= '9')
count++;
else
return -1;
}
if (count != 8)
return -1;
else
return (atoi (input));
}
int
validate_projectid (int project_id)
{
FILE *fptr;
project_detail *temp;
int found = 0;
fptr = fopen ("PROJECT_DETAILS.DB", "r");
if (fptr != NULL)
{
fread (temp, sizeof (project_detail), 1, fptr);
while ((found == 0) && (!feof (fptr)))
{
if (temp->project_ID == project_id)
found = -1;
fread (temp, sizeof (project_detail), 1, fptr);
}
fclose (fptr);
}
return ((found >= 0) ? 1 : -1);
}
void
generation_of_project_details_add ()
{
project_detail proj_details;
char choice;
int search = 0;
FILE *f_ptr;
/*taking all the value from the keyboard */
do
{
/* Checking validity of Project detail ID */
do
{
printf ("\n Give the identification number of project\n");
scanf ("%s", proj_details.project_id);
fflush (stdin);
proj_details.project_ID = get_valid_int
(proj_details.project_id);
if (proj_details.project_ID == -1)
{
printf ("Invalid number. Pls enter again.\n");
continue;
}
search = validate_projectid (proj_details.project_ID);
/* Give error message if input is invalid */
if (proj_details.project_ID == -1 || search == -1)
printf
("\n The given input is invalid... Please try again...
\n");
}
while (proj_details.project_ID == -1 || search == -1); /* end
while
loop(inner) */
f_ptr = fopen ("PROJECT_DETAILS.DB", "a");
fwrite (&proj_details, sizeof (proj_details), 1, f_ptr);
fclose (f_ptr);
printf
("Do you want to input any other project detail. Press any key
to continue and press x to exit");
scanf (" %c", &choice);
}
while (choice != 'x');
}
int
main ()
{
generation_of_project_details_add ();
return 0;
}