Thread: adding onto strings

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    adding onto strings

    Hi all

    What i would like to know is best shown with an example.

    The user is about to save a file. The user types in "file1" as the files name. now with that I want to add a .dg onto the end of it. How would I do this?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use strcat()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
            char file_name[1024];
    
            /* Read the file */
            printf( "Enter a file name (no extension): " );
            fgets( file_name, 1024, stdin );
            file_name[strcspn( file_name, "\n" )] = '\0';
    
            /* Append the extension */
            strcat( file_name, ".dg" );
    
            /* Check that it worked right */
            printf( "%s\n", file_name );
    
            return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Dr. Bebop
    Code:
    file_name[strcspn( file_name, "\n" )] = '\0';
    Nice one. Got that from Hammer?

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Yep, it makes for cleaner looking code.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    thanks

    Yep got it working thanks

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Nice one. Got that from Hammer?
    Glad to see the code is being used But I can't and won't take the credit for this one liner. I first saw it used by Prelude, and thought the same as Dr Bebop.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Strings to ListBox in a Dialog
    By kevins963 in forum C Programming
    Replies: 5
    Last Post: 09-10-2008, 04:36 PM
  2. adding strings?
    By Led4urhead123 in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2008, 03:13 PM
  3. adding strings hard problem...
    By qubit67 in forum C Programming
    Replies: 28
    Last Post: 04-22-2007, 02:02 AM
  4. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  5. Adding two strings to make a wchar_t
    By ElWhapo in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2005, 11:36 PM