Here is this function i'm testing to create a .txt file, then later-on add strings to it, its basically a database. When the program executes, it crashes after inputting the DB name. I suspect its the pointer, '*p', that is the fault of the run-time error, but its just an assumption.
Please, could someone review the code over and tell me what is making it crash upon execution?
Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXREC 50 //Maximum records /might delete 'em if they serve no other perpose
#define MAXCHAR 100 //Maximum chars /to give a records[][] a defined max value
void mkdBase(void); //Change prototypes!!!!... later
struct Recbase
{
char records[MAXREC][MAXCHAR];
char *delrec;
char dbasenm[11];
char title[MAXCHAR + 1];
}dbase,titre;
void main()
{
printf("Welcome to the DBMS\n");
mkdBase();
exit(1);
}
void mkdBase(void) //creates a text file, which is essentially the DBMS
{ // and text is inputted in the .txt. file
FILE *fp;
char *p;
printf("\nWelcome to the database maker\n");
printf("\nWhat would you like to name your database [10 char's max]:\n");
scanf("%s", &titre.dbasenm);
p = titre.dbasenm;
p = (char *)malloc(6*sizeof(char)); //create enough space for ".txt"
strcat(p,".txt"); // copy ".txt" into p
if(fp = fopen(p,"w")==NULL)
{
printf("Cannot Create file\n");
exit(1);
}
printf("\nPlease specify the title of your DBMS[100 char's max]:\n");
scanf("%s",&titre.title);
fprintf(fp,"\t%s", titre.title);
fclose(fp);
}