Thread: trying to remove " off end of string

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    trying to remove " off end of string

    Gudday
    Still trying to catch up C programming after no activity for a decade.
    I want to strip a trailing " off a string so that the string can be used to move a file but the program is crashing
    The string in question (known as tiffUrl) looks like
    Code:
    \\Au-mobius-e\E20-120_Scan\PICKLIST\0000544A.TIF"
    The " at the end is the problem.
    I thought that copying the string, except for the last char (the ") would do the trick but alas it does not work

    Code:
    char *tiffUrl="";   //string contains where TIFF file will end up
    char *tmp="";     //temporary holder to remove trailing " in tiffUrl
    
    ....
    
    tiffUrl= (fileline + strlen(fileline) - (strlen(fileline)-index));
    printf("Length of tiffUrl is %i\n", strlen(tiffUrl));
    for(x=0;x<(strlen(tiffUrl)-1);x++) {
       printf("x=%i\n", x);
       tmp[x] = tiffUrl[x];
    }
    printf("new tiffUrl is %s\n", tmp);
    The code prints out x =0 ok then crashes.

    [once I get the code working I was going to copy tmp back to tiffUrl using
    Code:
    tiffUrl = tmp;
    Is this ok or should I do use strcpy?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I couldn't quite follow what you were doing in that code, but is this what you are looking for?
    Code:
    char string[] = "\\Au-mobius-e\E20-120_Scan\PICKLIST\0000544A.TIF\""
    size_t length = strlen(string);
    if(string[length-1] == '\"') /* If the last character is a quote... */
        string[length-1] = '\0';  /* Remove the last character */

  3. #3
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Sorry I made it so obtuse.

    I know that the file will have the " at the end. There is previous work that is unseen that makes it happen.
    As long as I can shorten the file by one (1) character, the ", then i will be happy.

    I will try your as it appears simpler that my effort.;

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you are just shortening a string by chopping off the last character, then it can be simplified down to:
    Code:
    string[strlen(string)-1] = '\0';

  5. #5
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Wouldn't it be strlen-2 rather than strlen-1?

    When I tried strlen-1 it appears to have trouble i.e no change in tiffUrl. When I try strlen-2 the " disappears. I assume that is because the new line character was strlen-1. I note that now the list of strings (more that one tiffUrl is being generated) when printed to a file are one contiguous string rather than being on a separate line as usual.

    Sign. Fix one problem and another appears.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you refer to the filenames being printed out one after the other, that indicates that your printf statement is incorrect. (If you want your file names separated by something, you must print that something out yourself instead of expecting it to magically happen.)

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Wouldn't it be strlen-2 rather than strlen-1?
    Only if you want to chop off the last 2 characters in the string instead of the last 1.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Another simple way to do it:

    Code:
    char* quote = strstr( data, "\"" );
    if( quote )
    	*quote = 0;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest strchr instead of strstr, or possibly strrchr if there may be quote characters in the middle of the string that should remain. (But if there may be such characters in the middle of the string, and yet there might not be such a character at the end to remove, then the strlen method would be correct.)
    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

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I suggest strchr instead of strstr, or possibly strrchr if there may be quote characters in the middle of the string that should remain. (But if there may be such characters in the middle of the string, and yet there might not be such a character at the end to remove, then the strlen method would be correct.)

    Good point.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. How to remove end spaces from string
    By nitinmhetre in forum C Programming
    Replies: 4
    Last Post: 12-16-2006, 01:14 AM
  5. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM