char-question

This is a discussion on char-question within the C Programming forums, part of the General Programming Boards category; Hi again. I want to seperate a QUERY_STRING of my Apache ( so CGI ) with C. It is in ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Arrow char-question

    Hi again.

    I want to seperate a QUERY_STRING of my Apache ( so CGI ) with C. It is in the form "arch=foo&status=bar", and I need "foo" in char arch[] and "bar" in char status[] to redirect users to a specific file... . This is what I did:
    Code:
    int i;
    char *seperate_arch(char string[],char outstring[]) {
    	int length = strlen(string);
    	for(i=4;i<=length;i++) {
    		if(string[i] == '&') break;
    		outstring[i] = string[i];
    	}
    global_i = i;
    return outstring;
    }
    char *seperate_status(char string[],char outstring[]) {
    	int length = strlen(string);
    	for(i=i++;i<=lenght;i++) 
    		outstring[i] = string[i];
    return oustring;
    }
    int main(void) {
    	char arch[255],status[255];
    	strcpy(arch,seperate_arch(getenv("QUERY_STRING"),arch));
    strcpy(status,seperate_status(getenv("QUERY_STRING"),status));
    
    (...)
    }
    arch contains nothing and status a "n" with a "~" ... So whats wrong ?

    greets, demonus

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one idea.
    Code:
    int i;
    char *seperate_arch(char string[],char outstring[]) {
    	int j = 0;
    	int length = strlen(string);
    	for(i=5;i<length;i++) {
    		if(string[i] == '&') break;
    		outstring[j++] = string[i];
    	}
    //global_i = i;
    outstring[j] = '\0';
    return outstring;
    }
    char *seperate_status(char string[],char outstring[]) {
    	int j = 0;
    	int length = strlen(string);
    	for(i+=8;i<length;i++) 
    		outstring[j++] = string[i];
    outstring[j] = '\0';
    return outstring;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    34
    hm. works, thanks a lot. I just wonder *why* it works because of changing one integer ... ( and the '\0' stuff I forgot )

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Why not have a more general function such as
    char *seperate(char string[],char outstring[], char *name)
    You'll find the strstr() function most useful here.

    Which you would call with
    seperate(getenv("QUERY_STRING"),arch,"arch=");
    seperate(getenv("QUERY_STRING"),status,"status=");

    > strcpy(arch,seperate_arch(getenv("QUERY_STRING"),a rch));
    This is a waste of effort. The separate function returns arch, so all you end up doing is the equivalent of
    strcpy( arch, arch );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Salem

    This is a waste of effort. The separate function returns arch, so all you end up doing is the equivalent of
    strcpy( arch, arch );
    Much worse, this is dangerous of one wants ones program to work correctly in different compilers. According to the standard
    The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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