Trimming end of string

This is a discussion on Trimming end of string within the C++ Programming forums, part of the General Programming Boards category; This rather small problem is proving a bit of a nuisance. I have a string that looks like this: Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27

    Trimming end of string

    This rather small problem is proving a bit of a nuisance. I have a string that looks like this:

    Code:
    a_long_string_with_lots_of_underscores

    I need to trim off the final "_underscores", and so thought that the following plan might work:

    Code:
    char fname[64];
    //a string is generated by another part of the code and
    //copied into fname with strcpy at this point
    int len = sizeof(fname) -1;
    int k;
    for (k=len;k>0;k--)
    {
      if (fname[k] == "_")
      {
        fname[k] = "\0";
        printf("String shortened to: %s\n",fname);
      }
    }
    The compiler doesn't like the comparison, or the assignment of "\0" to an array element. Would anyone mind telling me why? Thanks.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You need single quotes ' rather than double quotes " when dealing with individual characters.

    QuantumPete

    Edit: Also, make sure that fname is initialised to 0 before copying in the string. Otherwise a random _ after the string has finished would throw off your algorithm. Better yet, instead of sizeof(), use strlen() and make sure that the string is properly NULL terminated before it is copied to fname.
    "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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    "something" is const char[n] or const char*, yet you are accessing individual elements of the array, thus they are char.
    A c-string is just an arrays of chars. Nothing special. It's important to know the difference.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    Quote Originally Posted by QuantumPete View Post
    You need single quotes ' rather than double quotes " when dealing with individual characters.
    D'oh!
    Thanks - other suggestions incorporated as well, and it is running nicely.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This whole thread is in the wrong forum - the code is exclusively using C features, and no C++ features at all.

    You probably should use:
    Code:
    int len = strlen(fname);
    instead of your sizeof() bits.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    Quote Originally Posted by matsp View Post
    This whole thread is in the wrong forum - the code is exclusively using C features, and no C++ features at all.

    You probably should use:
    Code:
    int len = strlen(fname);
    instead of your sizeof() bits.
    I'm using strlen() now, thanks.
    The reason I posted it here is that I'm obliged to compile what I'm doing as C++ (other work-related requirements) even though I know even less C++ than I do C (which is little enough). So, if there's a better C++ way of doing something I might as well use that.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by knirirr View Post
    The reason I posted it here is that I'm obliged to compile what I'm doing as C++ (other work-related requirements)...
    But that's not a bad thing. Any C code compiled in C++ is better than compiled with a C compiler. And I'm referring to the workings of the compiler and not the end result. A C++ compiler is so much safer and better than any C compiler.

    ...So, if there's a better C++ way of doing something I might as well use that.
    Oh, but there's plenty of that, believe me.
    To begin with, you should be using std::string and not char.
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by knirirr View Post
    I'm using strlen() now, thanks.
    The reason I posted it here is that I'm obliged to compile what I'm doing as C++ (other work-related requirements) even though I know even less C++ than I do C (which is little enough). So, if there's a better C++ way of doing something I might as well use that.
    Well, that obviously depends on what your overall goal is. C++ has "std::string" which would be suitable for doing string related work, and you could use find/find_first_of to locate where in the string your _ is, then using erase() to remove what's after the _.

    But if what you need later is a modifiable char array [C style string], then you would not benefit much by making it a C++ std::string, only to have to copy it back out to a C style string a little bit later on.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Elysia View Post
    A C++ compiler is so much safer and better than any C compiler.
    There are subtle differences between C and C++ compilers, which means always compiling your C code with a C++ compiler will eventually make it unportable.

    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    If it weren't for that some systems do not have C++ compilers, I fail to see where C is necessary.
    C++ can do all C can do and more, just as efficient as C or more efficient than C.
    Really, we are living in the past.
    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.

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Elysia View Post
    C++ can do all C can do and more, just as efficient as C or more efficient than C.
    I'm not getting into a slugging match with you over what compiler is better, but your "C++ is the Holy Grail of Programming" attitude is getting annoying.

    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I am suggesting that we don't need C and C++. We need only one.
    C++ is not the holy grail, but it deprecates C.
    C++ has its purpose. C has lost its.
    C#, Java, VB, and all other languages... has a different purpose.
    There is no holy grail.
    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.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I am suggesting that we don't need C and C++. We need only one.
    C++ is not the holy grail, but it deprecates C.
    C++ has its purpose. C has lost its.
    C#, Java, VB, and all other languages... has a different purpose.
    There is no holy grail.
    Yes, you keep saying that. I have, like QuantumPete, got a bit tired of hearing it.

    1. C++ compilers are more strict on prototyping and type conversion. Most of the difference is things that give warnings in C are now errors in C++. Using compiler options like -Werror in gcc will remove almost all of those differences.

    2. C++ as a LANGUAGE is not noticeably safer than C. Using the correct C++ STL classes such as std::string is quite clearly safer than using char arrays for strings. But if someone wrote a sufficiently comprehensive C string library [that is not based directly on char arrays, but uses another way to describe string, e.g. a
    Code:
    struct { char *s; int len };
    then it could be made just as safe as C++ std::string. And I'd be very surprised if such a string library was not already in existence.

    3. Both languages require a programmer to understand what he/she is doing to write safe code. Code is not made safe by using a different compiler, it is made safe by using safe functions and safe coding practices. It may be helped by having a standard library set of functions/classes/templates that are safe - but that applies regardless of whether it is C or C++.

    4. C is still a language used for many things, including most drivers and kernel code in most of the current OS's. Being able to use a C compiler to compile existing code that does not compile in C++ [and I'm not talking about places where code is not ok C++ because it does tricky or dodgy stuff, but for example using "new", "operator", "class", "private", etc as identifier names in the code]. Modifying code that has a million lines or more in it, to comply with C++ compiler for the ONLY reason that someone decided that C is no longer a valid language is not a good business strategy.

    There are valid reasons to use a C++ compiler to compile C code. There are valid reasons to not do so.

    Compilers and libraries are tools. They solve certain types of problems to a greater or lesser efficiency. Choosing which to use where is part of our professional knowledge, just like a bricklayer knows when to use a big trowel (murslev in Swedish), and when to use a small one to portion out the mortar to build a brick wall. You can of course build all types of brick walls with only one small (or large) trowel - but it's not going to be the best way to do it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Amen
    "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

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by matsp View Post
    Yes, you keep saying that. I have, like QuantumPete, got a bit tired of hearing it.
    And I'm sick and tired of old and unsafe C compilers which allows all kinds of stupid things.
    I'm also sick of how there's TWO languages that serve the same purpose, mostly because they interfere with each other.

    1. C++ compilers are more strict on prototyping and type conversion. Most of the difference is things that give warnings in C are now errors in C++. Using compiler options like -Werror in gcc will remove almost all of those differences.
    Unfortunately, it will not remove certain flaws such as implicit function calls in all places, and that warrants enough to give it criticism for something that should never have been allowed in the first place.

    2. C++ as a LANGUAGE is not noticeably safer than C. Using the correct C++ STL classes such as std::string is quite clearly safer than using char arrays for strings. But if someone wrote a sufficiently comprehensive C string library [that is not based directly on char arrays, but uses another way to describe string, e.g. a
    Code:
    struct { char *s; int len };
    then it could be made just as safe as C++ std::string. And I'd be very surprised if such a string library was not already in existence.
    And I find it a real shame that no such library has been incorporated into the standard yet, although there is a proposal for one such library.
    I would be much happier once such a library makes it into the standard, believe me.

    4. C is still a language used for many things, including most drivers and kernel code in most of the current OS's. Being able to use a C compiler to compile existing code that does not compile in C++ [and I'm not talking about places where code is not ok C++ because it does tricky or dodgy stuff, but for example using "new", "operator", "class", "private", etc as identifier names in the code]. Modifying code that has a million lines or more in it, to comply with C++ compiler for the ONLY reason that someone decided that C is no longer a valid language is not a good business strategy.
    Please note that I never suggest that ALL code should be recompiled with C++. There a reason why backwards compatibility exists. Legacy code should be compiled with legacy support.
    New code should be compiled with new newer tools.

    And I am also sick and tired of people writing PC applications in C. It's obviously WRONG because of how much the quality degrades. Memory leaks, security problems, etc.
    Had they just written the damn program in C++ or C# or VB or whatever, there would be no memory leaks or security problems.
    And if speed is important, then they could have used C++ instead of C, and gotten the best of both worlds.
    C++ was designed to be today's C, not C#. So why do we need yesterday's C (for new projects)?
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM

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