c gurus

Discussion in 'OT Technology' started by synthetic, Sep 27, 2006.

  1. synthetic

    synthetic New Member

    Joined:
    Apr 9, 2002
    Messages:
    5,704
    Likes Received:
    0
    Location:
    San Diego, CA
    we know argv comes in as a pointer, i get only warnings of comparing pointer to interger when i compile......

    int parse_cnt=0;
    char read[256];
    char tech[256];
    char *ptech;

    strcpy(read, argv[2]);

    while (strchr(read[parse_cnt],'/') != '/' ){
    tech[parse_cnt] = read[parse_cnt];
    parse_cnt++;
    }
    /* cop back in as ptr */
    strcpy(ptech, tech);
    printf("\nTech: %s ",ptech);

    well when i run it this piece of code crashes all.....
    basicly, i want to be able to split argv[2] into two char variables when i see an "/" ... what would be the best method?
     
    Last edited: Sep 27, 2006
  2. synthetic

    synthetic New Member

    Joined:
    Apr 9, 2002
    Messages:
    5,704
    Likes Received:
    0
    Location:
    San Diego, CA
    int make_call_to_ana=0, i=0, j=0, splitc=0;
    char *ptech_to_ana, *pdest_to_ana;

    if (argc > 2 || argc == 1)
    return RESULT_SHOWUSAGE;
    else{
    printf("\n //////COMMAND UNDER CONSTRUCTION/////// \n");

    while (argv[2] != '\0')
    {
    if (splitc == 0)
    {
    if (argv[2] == '/')
    {
    splitc = 1;
    ptech_to_ana = '\0';
    }
    else
    {
    ptech_to_ana = argv[2];
    }
    }
    else
    {
    pdest_to_ana[j] = argv[2];
    j++;
    }
    i++;
    if (i>10 && splitc==0)
    break;
    }
    if (splitc==1){
    printf("\nTech: %s Dest: %s \n",ptech_to_ana,pdest_to_ana);
    }
    else
    return RESULT_SHOWUSAGE;

    changed it a bit still no luck.....
     
  3. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    Here is what I came up with. I suck at C so there is probably a better way to do this.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	char read[256];
    	char tech[256];
    	char *ptech;
    
    	strcpy(read, argv[1]);
    
    	ptech = strchr(read,'/');  // get pointer to '/'
    	strcpy(tech, ptech+1);  // copy stuff after '/' to tech
    
    	ptech[0] = '\0';  // terminate read string at '/'
    	printf("read: %s\n", read);
    	printf("ptech: %s\n", tech);
    
    	return 0;
    }
    
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    char *parta;
    char *partb;
    strcpy(read, argv[2]);
    parta = read;
    partb = strtok(read, "/");
     
  5. synthetic

    synthetic New Member

    Joined:
    Apr 9, 2002
    Messages:
    5,704
    Likes Received:
    0
    Location:
    San Diego, CA
    passing argument 2 of âstrtokâ makes pointer from integer without a cast


    and it still crashes....
     
  6. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    give me the command line youre using
     
  7. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        char read[256];
        char firstPart[100];
        char secondPart[100];
        char *ptech;
    
        strcpy(read, argv[2]);
    
        ptech = strchr(read,'/');
        *ptech = '\0';
        strcpy(firstPart,read);
        strcpy(secondPart, ptech+1);  
    
        printf("First Part: %s\n", firstPart);
        printf("Second Part: %s\n", secondPart);
    
        return 0;
    }
    
    That gives you both halves as null terminated strings in firstPart and secondPart. I compiled this in MS VC++ 6.
     
  8. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    Make sure you are giving it enough command line arguments. If you are not that can cause a segmentation fault.
     
  9. synthetic

    synthetic New Member

    Joined:
    Apr 9, 2002
    Messages:
    5,704
    Likes Received:
    0
    Location:
    San Diego, CA
    well at least it is not giving me that warning anymore thanks to gogzilla.... yet something in the search for '/' causes the rpogram to crash.....

    test input i use... ./prog cmd test/123
     
  10. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Did you use single quotes or double quotes around the slash?
    It's supposed to be double quotes so that it is a literal string, not a char.
     
  11. synthetic

    synthetic New Member

    Joined:
    Apr 9, 2002
    Messages:
    5,704
    Likes Received:
    0
    Location:
    San Diego, CA
    with double quotes....

    app_analyze.c:72: warning: passing argument 2 of â__rawmemchrâ makes integer from pointer without a cast
    app_analyze.c:72: warning: passing argument 2 of â__builtin_strchrâ makes integer from pointer without a cast


    without i get no warnings.
     
  12. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    D:\C++\OT\Debug>OT.exe cmd test/123
    First Part: test
    Second Part: 123

    D:\C++\OT\Debug>

    do a printf("%s", argv[2]) and see what you get. As you can see I have no problems so I dunno what's going on over there.
     
  13. synthetic

    synthetic New Member

    Joined:
    Apr 9, 2002
    Messages:
    5,704
    Likes Received:
    0
    Location:
    San Diego, CA
    wierd its showing up as argv[1] but it is out of line with argc's count. , and at that note... "GREAT SUCCESS!!" - borat.

    thanks guys
     

Share This Page