943,686 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2596
  • C RSS
Nov 4th, 2007
0

erase file and create a random string

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
Nov 4th, 2007
0

Re: erase file and create a random string

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: BASIC Editor: Where Did I Go Wrong?
Next Thread in C Forum Timeline: include exe in filehandling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC