V
Vinicio Flores
Hello C hackers, this program that I am writing print "Segmentation
fault" when I execute it, I think that the problem is the gets
function, but I am not sure
/** A simple database system **/
#include "datbase.h"
static int search_and_open_data_base(char *Arg)
{
FILE *f;
f = fopen(Arg, "r");
if(!f)
return 1;
else
return 0;
}
/* ************************************************ */
static FILE *create_database(char *arg)
{
FILE *newbase;
newbase = fopen(arg, "w+");
return newbase;
}
/* ************************************************ */
static int print_options()
{
clrscr();
setattr(REVERSE);
setcolor(FOREBLUE);
gotoxy(2, 2);
cprintf("DatBASE");
gotoxy(2, 4);
setattr(NORMAL);
setcolor(FOREGREEN);
cprintf("I)Insert new register R)Print a register S) Exit");
gotoxy(2, 6);
setcolor(FORERED);
cprintf("Option: ");
setattr(NORMAL);
normvideo();
}
/* ************************************************ */
static int open_and_fill_database(char *arg)
{
FILE *database;
struct datbase_registers *base = malloc ( sizeof(struct registers
*));
database = fopen(arg, "ra");
gotoy(10);
puts("Insert register name: ");
gets(base->regname);
gotoy(11);
puts("Insert name: ");
gets(base->name);
gotoy(12);
puts("Insert telephone number: ");
scanf("%ld", base->ntel);
gotoy(13);
puts("Insert age: ");
scanf("%ld", &base->age);
fprintf(database, "Register name: ");
fputs(base->regname, database);
fprintf(database, "\n\nPerson's name: ");
fputs(base->name, database);
fprintf(database, "\nTelephone number: %lf\n", base->ntel);
fclose(database);
}
/* ************************************************ */
static int get_and_process_input()
{
int ch;
ch = getch();
switch(ch)
{
case 'i':
case 'I':
open_and_fill_database("data.base");
break;
case 'r':
case 'R':
break;
case 's':
case 'S':
setattr(NORMAL);
normvideo();
reset();
ret;
break;
default:
gotoy(9);
cprintf("?Unknown operation\n");
sleep(1);
print_options();
get_and_process_input();
break;
}
}
/* ********************|*************************** */
/* --------------------|--------------------------- */
/* ************| MAIN FUNCTION |******************* */
int main()
{
FILE *database;
int c;
int search = search_and_open_data_base("data.base");
if(search == 1)
create_database("data.base");
else
{
print_options();
get_and_process_input();
}
return 0;
}
Here is the datbase.h C header:
/** A simple database system **/
#include <ttk.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX ((900*800)*256)
struct datbase_registers
{
char regname[MAX];
char name[MAX];
long int ntel;
long int age;
};
fault" when I execute it, I think that the problem is the gets
function, but I am not sure
/** A simple database system **/
#include "datbase.h"
static int search_and_open_data_base(char *Arg)
{
FILE *f;
f = fopen(Arg, "r");
if(!f)
return 1;
else
return 0;
}
/* ************************************************ */
static FILE *create_database(char *arg)
{
FILE *newbase;
newbase = fopen(arg, "w+");
return newbase;
}
/* ************************************************ */
static int print_options()
{
clrscr();
setattr(REVERSE);
setcolor(FOREBLUE);
gotoxy(2, 2);
cprintf("DatBASE");
gotoxy(2, 4);
setattr(NORMAL);
setcolor(FOREGREEN);
cprintf("I)Insert new register R)Print a register S) Exit");
gotoxy(2, 6);
setcolor(FORERED);
cprintf("Option: ");
setattr(NORMAL);
normvideo();
}
/* ************************************************ */
static int open_and_fill_database(char *arg)
{
FILE *database;
struct datbase_registers *base = malloc ( sizeof(struct registers
*));
database = fopen(arg, "ra");
gotoy(10);
puts("Insert register name: ");
gets(base->regname);
gotoy(11);
puts("Insert name: ");
gets(base->name);
gotoy(12);
puts("Insert telephone number: ");
scanf("%ld", base->ntel);
gotoy(13);
puts("Insert age: ");
scanf("%ld", &base->age);
fprintf(database, "Register name: ");
fputs(base->regname, database);
fprintf(database, "\n\nPerson's name: ");
fputs(base->name, database);
fprintf(database, "\nTelephone number: %lf\n", base->ntel);
fclose(database);
}
/* ************************************************ */
static int get_and_process_input()
{
int ch;
ch = getch();
switch(ch)
{
case 'i':
case 'I':
open_and_fill_database("data.base");
break;
case 'r':
case 'R':
break;
case 's':
case 'S':
setattr(NORMAL);
normvideo();
reset();
ret;
break;
default:
gotoy(9);
cprintf("?Unknown operation\n");
sleep(1);
print_options();
get_and_process_input();
break;
}
}
/* ********************|*************************** */
/* --------------------|--------------------------- */
/* ************| MAIN FUNCTION |******************* */
int main()
{
FILE *database;
int c;
int search = search_and_open_data_base("data.base");
if(search == 1)
create_database("data.base");
else
{
print_options();
get_and_process_input();
}
return 0;
}
Here is the datbase.h C header:
/** A simple database system **/
#include <ttk.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX ((900*800)*256)
struct datbase_registers
{
char regname[MAX];
char name[MAX];
long int ntel;
long int age;
};