string help

This is a discussion on string help within the C++ Programming forums, part of the General Programming Boards category; in my program i use the strcat(name, ".txt") to add .txt to the name imputed and to save it as ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    string help

    in my program i use the strcat(name, ".txt") to add .txt to the name imputed and to save it as a text file.

    well later when i use it to call that same file back how do i remove the .txt so that the name can be used for something else.

    thanks for any help.
    Hooked On Phonics Didn't Work For Me!

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Re: string help

    Originally posted by jobolikescake
    in my program i use the strcat(name, ".txt") to add .txt to the name imputed and to save it as a text file.

    well later when i use it to call that same file back how do i remove the .txt so that the name can be used for something else.

    thanks for any help.
    Try this.

    Code:
    //other code goes here (before the strcat)
    char name2[strlen(name)];
    strcpy(name2,name);
    strcat(name, ".txt");
    Then just use name2 when you want the original name

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    you can use std::string and all your troubles will be gone,

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char name2[strlen(name)];
    strcpy(name2,name);
    strcat(name, ".txt");
    Oops, you didn't get the size of the name2 array correct, and you blew the buffer. To explain: you made name2 long enough to hold the characters within name, but you forgot about the null terminator, and also the .txt chars, so they're off in someone elses memory.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    I'm kinda fuzzy on how this works, buit i believe you can use strstr() to find out where ".txt" is in the string. If you used the STL string class, you could then use erase() them.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you have a char array holding your string, and it looks something like this:

    >>filename.txt

    then all you need to do to remove the .txt is replace the . with a \0. So, find a way of getting a pointer to the dot. You could use strstr(), strchr(), or strlen()-4. This is all a C way of doing things though, maybe you should try something more C++ like.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    just using the strcpy(name1, nameone) works just fine by its self

    thanks for the help, that was the one part of my program that i couldnt get right, but now that i have it now ive finished my program.
    Hooked On Phonics Didn't Work For Me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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