Thread: Writing random strings to file; occasional gibberish. What's wrong?

  1. #1
    Registered User Stellar's Avatar
    Join Date
    Dec 2016
    Posts
    2

    Writing random strings to file; occasional gibberish. What's wrong?

    Greetings and welcome to my first post. My objective is to generate 1000 random strings of lowercase letters of random length 3-9, and write them to a file. It seems like I've accomplished all of this, but there's a problem.

    It looks like whenever a random 9-character string is generated/printed, it has this [*little box*]« at the end.

    Example: mkpvcq xicleyjip[*little box*]« abqwn lmpjgswtq[*little box*]«

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <strings.h>
    int main(){
        int i = 0, j = 0;
        int numWords = 1000;
        int length;
    
    
        FILE *fout;
        fout = fopen("words.txt","w");
        
        for( i = 0; i < numWords; i++ ){
            length = (3 + rand() % 7);
            char word[length];
            for( j = 0; j < length; j++ ){
                word[j] = 'a' + (rand()%('z'-'a'));
            }
            fprintf(fout, "%s ", word);
            memset(word, 0, length);
        }    
    
    
        fclose(fout);
        return 0;    
    }
    (t is required that they are saved as strings before being written to the file, not just put directly as individual characters)

    How do I fix this? Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You aren't making the array large enough to hold a C-style string with the given length. A C-string needs room for a null character at the end, so your array should be of size length+1, and you should add a null character at the end before printing it. The "little box" is just an unknown character code. The memset is useless since word goes out of scope right after that.

    You will never get a 'z' the way you have it. It should be:
    Code:
    word[j] = 'a' + rand() % ('z' - 'a' + 1);
    is required that they are saved as strings before being written to the file, not just put directly as individual characters
    Of course you can write the individual characters to the file. Look up fputc.

  3. #3
    Registered User Stellar's Avatar
    Join Date
    Dec 2016
    Posts
    2
    Thanks for the clear response! Makes sense.

    Quote Originally Posted by algorism View Post

    Of course you can write the individual characters to the file. Look up fputc.
    The old test question that I'm studying arbitrarily made that a rule, is what I meant

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I forgot to mention that you need to call srand to initialize the seed for rand. Otherwise you'll get the same results every run.

    Also, you could use some defines for your "magic numbers" and a function to return a random integer in a given range.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUM_WORDS   10
    #define MIN_WORD    10
    #define MAX_WORD    50
    #define LOW_CHAR    '0'
    #define HIGH_CHAR   '1'
    
    int rndrange(int low, int high) {  // high must be >= low
        return low + rand() % (high - low + 1);
    }
    
    int main() {
        srand((unsigned)time(NULL));
    
        FILE *f = stdout; // to stdout for testing
        char word[MAX_WORD + 1];
    
        for (int n = 0; n < NUM_WORDS; n++) {
            int len = rndrange(MIN_WORD, MAX_WORD);
    
            for (int i = 0; i < len; i++)
                word[i] = rndrange(LOW_CHAR, HIGH_CHAR);
            word[len] = '\0';
    
            fprintf(f, "%s\n", word);
        }
    
        fclose(f);
    
        return 0;
    }
    Even better, you could receive the output filename and other parameters from the command line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Data Randomly to a Random-Access File
    By lmanukyan in forum C Programming
    Replies: 3
    Last Post: 02-12-2016, 08:12 AM
  2. Replies: 10
    Last Post: 02-29-2012, 04:56 PM
  3. Program writing gibberish to a .dat file
    By wisdom30 in forum C Programming
    Replies: 22
    Last Post: 05-15-2011, 05:51 PM
  4. Replies: 4
    Last Post: 08-07-2007, 06:07 PM
  5. Writing a set of random integers to a file
    By RazielX in forum C Programming
    Replies: 19
    Last Post: 09-17-2004, 07:00 PM

Tags for this Thread