Thread: Strcat with filenames

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Question Strcat with filenames

    I get a name from the user and output it to a file "name.txt". For instance, if the user entered 'tofugirl' I write to a file called "tofugirl.txt". However, how would I get "name" ("tofugirl") back since it is concatinated to the ".txt"? Is there an "un concatinate'?

    Any help greatly appreciated. Code at the bottom.

    Code:
    char *name;
    FILE *ofp;
       
    
       printf("Please enter name\n");
       scanf("%s", name);
    
       strcat(name, ".txt");
    
       ofp = fopen(name, "w");
       
       if(ofp == NULL)
       {
          printf("Couldn't open %s\n", name);
          exit(-3);
       }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Your name pointer should be set to point to something.
    If your always going to be adding 4 chars (.txt) then you can just use strlen to get the length of the string and either copy that many minus 4 to another string or change the '.' to a NULL.
    A more rubust way would be to create a function that scans your string from the end to begining looking for the '.'
    Last edited by Quantum1024; 11-07-2005 at 08:43 PM.

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Why not just strcpy the original onto another variable named something like nameorig, before you append .txt to it? That way you have a variable that is a valid filename, and a variable that is just the name they entered.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Do you perhaps have a code snippet for this? I tried to do that but I'm getting core dumps. Here's my code.

    Code:
    char *name, *origname;
    FILE *ofp;
       
    
       printf("Please enter name\n");
       scanf("%s", name);
      strcpy(origname, name);
       strcat(name, ".txt");
    
       printf("Original Name: %s\n", origname);
       ofp = fopen(name, "w");
       
       if(ofp == NULL)
       {
          printf("Couldn't open %s\n", name);
          exit(-3);
       }

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You're getting core dumps because you didn't listen to the initial reply to your original post.

    Code:
    char *name;
    This declares enough space to hold a pointer.

    Code:
    scanf("%s", name);
    This reads in a string, and puts it where name points to. Where does name point to?

    Code:
    strcpy(origname, name);
    This copies the string that name points to, to the address that origname points to. Where does origname point to?

    You need to allocate space to store strings, either by using a pointer and malloc, or using an array of chars.

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah, try initializing the two char variables like this:
    Code:
    char name[256]={0};
    char origname[256]={0};
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Thanks for all your suggestions. I went with jmd15's answer. It works now.

    A big thank you!

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Or you can use strncpy,

    Code:
    strncpy(name,filename,(strlen(filename)-4));
    Or

    strtok:

    Code:
    name = strtok(filename, ".");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 02:49 AM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM