erase file and create a random string

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

erase file and create a random string

 
0
  #1
Nov 4th, 2007
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:
  1. char *b="";
  2. int i,j;
  3. FILE *f=fopen(file,"w");
  4. for(i=0;i<4;i++)
  5. { for(j=0;j<7;j++)
  6. *b+=char(random(27)+65);
  7. fprintf(f,"%s\n",&b);
  8. }
  9. fclose(f);
But the result is wrong
Thank you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,461
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: erase file and create a random string

 
0
  #2
Nov 4th, 2007
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.
Last edited by Ancient Dragon; Nov 4th, 2007 at 9:34 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC