Thread: Best way to find, extract, replace, etc in char strings?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Best way to find, extract, replace, etc in char strings?

    I could never figure out an easy way to handle strings in C. I need to find and extract part of a string up to the last occurrence of a particular character, and probably find/replace. I assume everything I need is in string.h, but I'm not having an easy time finding examples of specific tasks. For example. strrchar() returns a pointer. How am I supposed to use that pointer to extract everything up to that character? strncpy() expects to be told the number of chars to extract, not a pointer to the last one.

    Also, is there a simple way to trim BS characters from the start and end of a strings? VB6 had a Trim() function that filtered out whitespace and other special chars(\t,\n, etc) from the beginning and end of a string.

    I'm currently working with my char strings declared "char stringname[maxlength];" I'm curious as to the difference between this and cString/LPSTR. I don't think I've ever seen a cString used in an example without having to malloc() the damn thing. I don't want to do that, since you always have to free the memory later. Or is cString like the MFC solution to Char*? I want to stay away from MFC.
    Last edited by Viper187; 06-09-2008 at 12:20 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you have
    char array[ ] = "hello.world";

    And
    char *p = strchr( array, '.' );

    Then you can do
    ptrdiff_t n = p - array;
    n in this example would be 5.

    You could then use that as a parameter to say strncpy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    uhhh "ptrdiff_t undeclared"
    ???

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    uhhh "ptrdiff_t undeclared"
    #include <stddef.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Still doesn't work.
    Code:
    char INIFile[MAX_PATH];
    		    if (GetModuleFileName(NULL,INIFile,sizeof(INIFile)) ) { 
                    char *fndchr = strrchr(INIFile,'\\');
                    ptrdiff_t chrloc = fndchr - INIFile;
                    strncpy(INIFile,INIFile,chrloc);

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Define does not work.

    strncpy(INIFile,INIFile,chrloc);
    This line is also suspect.
    Not only would it be undefined, but you're copying the same data into the buffer that's already in it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I think what you're doing there is a big old no-op.

    If you're looking to lop off the file part of the INIFile variable, then:

    Code:
    char INIFile[MAX_PATH] = { 0 };
    if (GetModuleFileName(NULL, INIFile, sizeof(INIFile)) ) { 
        char *fndchr = strrchr(INIFile, '\\');
        if (fndchr)
        {
             *fndchr = '\0';
             // If you want to keep the backslash use
             // *(fndchr + 1) = '\0';
        }
    }

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Thanks. Now I guess I need to setup a function to parse user input strings and attempt to assemble them. That should be fun.

    How the hell do people do this stuff?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, parsing simple input is not difficult at all. But it's more difficult in C, than higher-level languages.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by Viper187 View Post
    How the hell do people do this stuff?
    With a padded keyboard and desk.

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Kudose View Post
    With a padded keyboard and desk.
    That's when you move on to working with floats...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. help with strings
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 08:20 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM