strcar keep closing my program

This is a discussion on strcar keep closing my program within the C Programming forums, part of the General Programming Boards category; i have made a program, i had code that said Code: void main() { char a[20]; char txt[20]; char final[50]; ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    20

    strcar keep closing my program

    i have made a program, i had code that said

    Code:
    void main()
    {
    char a[20];
    char txt[20];
    char final[50];
    
    printf("please enter file name: ");
    scanf("%s,a);
    strcat(final , a); //with out this it works
    strcat(final, txt ); //and this
    file = fopen(a,"w");
    fprintf(file,"%s",a);
    }
    im not online with this computer so ive just writen out a similar problem, well its the same really.

    the program ends its self when i add in the strcat statments. without its fine.

    does anyone know why this is?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    The txt[] array is not initialized. You are copying garbage around.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    i no this wont work on its own, but i hanent got a memory stick to copy the actual code across, an im not online with the computer that im writing it on, i was just wondering if anyone has had a problem where when you try and use strcat it end the program where strcat is.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    char* strcat( char* dest, const char* src)
    The way the strcat function works is that it copies the string src to the end of the string dest. That is the key, it does this by first walking along the destination string until it finds a null (the end point of the destination string) and then copying the character from the source string to that point. When you call strcat the first time, strcat(final , a), final is uninitialized and the function tries to find the end of the string which is supposed to exist there. Eventually it will find a null to stop at after who knows how many bytes it has read through and then start copying the string in a. Since this location where it is copying data to is unknown, you are probably overwriting something important and your program crashes. A solution to this is to initialize first by perhaps using strcpy instead of strcat. Also, as mentioned, txt is uninitialized as well so the second call to strcat is unadvised.

    I think perhaps you meant to do this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char a[20];
        char final[50];
        FILE * file;
    
        printf("please enter file name: ");
        scanf("&#37;s",a);
        strcpy(final , a);
        strcat(final,".txt");
        file = fopen(final,"w");
        fprintf(file,"%s",a);
    
        fclose(file);
        return 0;
    }
    Last edited by hk_mp5kpdw; 07-26-2007 at 01:53 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    thanks alot, it works now, i put
    Code:
    final[0]='\0';
    thanks for that explanation, very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  2. exiting and closing a program
    By major_small in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 09:31 PM
  3. How to restart a program without closing it...
    By D4050 in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2001, 12:38 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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