Weird run-time error while testing this function

This is a discussion on Weird run-time error while testing this function within the C Programming forums, part of the General Programming Boards category; Here is this function i'm testing to create a .txt file, then later-on add strings to it, its basically a ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    35

    Weird run-time error while testing this function

    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);
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    malloc

    p = titre.dbasenm;
    p = (char *)malloc(6*sizeof(char)); //create enough space for ".txt"
    strcat(p,".txt");

    during assignment p is assigned some address let's say 1000 of course in proper hex format...
    anyway... Then you call malloc to allocate memory... the system has to go and find some other address so when you call strcat...
    the pointer is no longer pointing at the struct member but somewhere else instead...
    get rid of the *p and strcat to titre.dbasenm instead

    strcat(titre.dbasenm, ".txt");

    or get rid of just the malloc call

    strcat(p, ".txt"); //because p is actually a dereference for titre.dbasenm

    Hope this helps????
    zMan

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    35
    Yup, thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 11:42 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21