Thread: Appending "

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Appending "

    Hi all,

    I have a string and I want to append (") at the beginning and the end of it to get:

    "String.....".

    This is my program which works fine but it needs "(s) ( becuase the required command is: cygpath -w "string").


    Code:
    void converter( char *cyg, char *win)
    {
    FILE *fp;
    
    char command[1000] = "cygpath -w ";
    
    strcat( command, cyg);
    
    fp = popen(command, "r");
    
    if (fp == NULL)
    {
      printf("Error in opening pipe");
      exit(1);
    }
    
    while(fgets(win, 1000, fp) != NULL)
    
    if (pclose(fp) == -1)
    {
      printf("Error is countered in closing the pipe ");
    }
    
    }
    If there is any unlogical implementation in the code, please let me know.

    Wait your help.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You could malloc() a new string large enough to hold the current one plus the quotation marks, and copy the string over.

    Otherwise, you'd have to shift the entire string over to make room for the first quotation mark.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Can't I append the first " before storing anything in the buffer, and then starts buffering after this and at the end appending the second "?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Mmm, yes, you can do that.

    If you don't need the string for anything else, that's the best way of doing it.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    okay, then I tried this approach and I failed can you please look at the code and help me.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char command[1000] = "cygpath -w \"";

    strcat( command, cyg);

    strcat( command, "\"");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    okay, how this can be done for "win" ? The second " can be added using strcat, but what about the first " ?

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    This seems to work and its doing what vart suggested.
    Code:
    #include <stdio.h>
    #include <string.h>
    #define size 100
    
    int main()
    {
        char command[size] = "cygpath -w \""; // <- first " is added here
        char string[size];
        
        fgets(string, size, stdin);
        string[strlen(string)-1]='\0';
        
        strcat( command, string);
    
        strcat( command, "\"");
        
        printf("%s", command);
        getchar(); 
    }

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    How about:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define size 100
    
    int main()
    {
        char command[size];
        char string[size];
        
        fgets(string, size, stdin);
        snprintf(command, size, "cygpath -w \"&#37;s\"", string);
        
        printf("%s\n", command);
        getchar(); 
    }

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Not the safest, but yeah it should work most of the time. You take a slight chance on overrunning your buffer, which might not be a problem I guess since command and string are next to each other on the stack......

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgets and snprintf - both check the array bounds, so where do you see the problem of buffer overrun?

    The problem here - is the \n char that should be removed from the strings after fgets
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Nope, I'm confusing myself. brewbuck is right. Apologies yet again (I really need to stop this. ).

    It'll work, but you'll truncate the command from string if the length is too large. Not really a major problem.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vart View Post
    The problem here - is the \n char that should be removed from the strings after fgets
    Ehh, yeah. Forgot about that, as I usually do. It's simple to write a function that strips that off.

  14. #14
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by vart
    fgets and snprintf - both check the array bounds, so where do you see the problem of buffer overrun?
    This sounds like you can get can get a buffer overflow by putting a big string into a printf(). I guess I'll have a look at what snprintf() does different then.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What I mistakenly thought was happening was that we were writing at an offset of command, but passing the size size, which could result in a buffer overrun.

    We should be safe with this.

    Incidentally, printf() is almost always always safe if you use it right. sprintf() just uses a printf()-sorta-means to write to a string. snprintf() does it safely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Opening a file for binary appending
    By spank in forum C Programming
    Replies: 1
    Last Post: 11-01-2007, 07:34 AM
  3. appending structure bin file
    By voodoo3182 in forum C Programming
    Replies: 8
    Last Post: 08-09-2005, 10:59 AM
  4. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  5. File input and appending to a string
    By LandMonster in forum C Programming
    Replies: 2
    Last Post: 12-04-2001, 01:32 PM