954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

erase file and create a random string

I have some problems:
- How can I erase a file?
- How can I create a random string has 8 letters?
This is my code to create a random string has 8 letters:

char    *b="";
    int        i,j;
    FILE    *f=fopen(file,"w");
    for(i=0;i<4;i++)
    {       for(j=0;j<7;j++)
             *b+=char(random(27)+65);
             fprintf(f,"%s\n",&b);
    }    
    fclose(f);

But the result is wrong
Thank you!

donaldunca
Light Poster
27 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

One huge problem is pointer b only points to a one-byte empty string which can not be expanded. Since you want a string of 8 characters then you have to allocate memory for it something like this char b[9] = {0}; . That will allocate memory for 8 characters plus a null terminator, and initialize all of them with 0.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You