string genrator

This is a discussion on string genrator within the C Programming forums, part of the General Programming Boards category; problem here it everytime it keeps genreting more and more not just the much as specify i want to genrate ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    string genrator

    problem here it everytime it keeps genreting more and more not just the much as specify i want to genrate only i dunno whats problem here :S
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    void string_genrator(char *buffer,int N)
    {
    	
    	int c;
    	size_t i;
    	srand(time(NULL));
    	for(i = 0; i < N; i++) {
    		c = 'a' + rand() % ('z' - 'a' + 1);
    		buffer[i] = c;
    	}
    	buffer[i]=0;
    	i++;
    }
    int main(void)
    {
        char in[100];
        char buffer[]="name";
        srand(time(NULL));
        for(;;) {
            string_genrator(in,4);
            strcat(buffer,in);
            printf("Okay thats buffer <<%s>> ",buffer);
            getchar();
            }
        return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elwad View Post
    problem here it everytime it keeps genreting more and more not just the much as specify i want to genrate only i dunno whats problem
    Quote Originally Posted by Cactus_Hugger View Post
    English. Clear, comprehensible English. What exactly is your packet sniff showing?
    You could at least try a little bit.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh i see it keeps adding to old buffer

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    but how can i fix this should i just null terminate it and assign it again ?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1) You keep concatenating the random 4-letter string onto the buffer.

    2) Said buffer doesn't have any room to append anything to it. char buffer[] = "test" means that you have an array of 5 characters (including the terminating 0) to play with.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i figured but should i add 0 terminator then before that save that name to another array then resiagn back at the start of the loop or is there better way to do it ?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, you need to reserve enough space to store the final string. Since your buffer is already full when it contain "name", it can't be added to without overwrting something.

    It is not clear what you are actually trying to do, but having an infinite loop adding to a buffer will pretty quickly run out of memory [ok, maybe not so quickly if you keep printing the string].

    --
    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.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i did del then rewrote it again
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    void string_genrator(char *buffer,int N)
    {
    	
    	int c;
    	size_t i;
    	srand(time(NULL));
    	for(i = 0; i < N; i++) {
    		c = 'a' + rand() % ('z' - 'a' + 1);
    		buffer[i] = c;
    	}
    	buffer[i]=0;
    	i++;
    }
    int main(void)
    {
        char in[100];
        char buffer[]="name";
        int i=0;
        srand(time(NULL));
        for(;;) {
            if(i>0)
                strcpy(buffer,"name");
            string_genrator(in,4);
            strcat(buffer,in);
            printf("Okay thats buffer <<%s>> ",buffer);
            getchar();
            buffer[0]=0;
            i++;
            }
        return 0;
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your buffer still only has space for 5 characters, which you stuff in there with the "name" you assign in the first place. Adding another four characters makes 9, which overwrites whatever else is after the string.

    Code:
            buffer[0]=0;
    is completely meaningless, as you soon after will set the string with strcpy(buffer, "name");

    --
    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.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i made it name[10] so it doesnt overwrite but should if when i strcpy it shouldnt it get buff over flow ?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by elwad View Post
    i made it name[10] so it doesnt overwrite but should if when i strcpy it shouldnt it get buff over flow ?
    Not sure what you are trying to say here. name[10] will be fine for what you have been doing [in fact, it's one char too long, but that's just being very pedantic].

    If you your reference to strcpy you are trying to say that if you copy a 5 character string (including the terminating zero) into a 10 char array without first initializing the string, it would somehow cause a problem, then no, it doesn't. strcpy SETS the string to whatever is in the second argument.

    --
    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.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i was asking abt strcpy anyways thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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