I managed to fix your code, but I basically had to switch over to std::string
s since they're so much easier for me to work with.
(Not that you have
to use them, but....)
Anyways, I can't even keep track of what I changed, so I'll just point out some of the bigger issues:
-- You're trying to create n
different files. That means you need n
different (random) file names. Currently, you're only generating one (ah
). By the way, that string isn't null-terminated -- you should probably fix that (a simple
ah = '\0'
So, instead of generating a (single) random filename now, generate them as you go, when you need them. (More on that later.)
By the way, also see my other post above about the srand
-- I would open your files (all of them) in binary mode (with ios::binary
). (Fixed a minor issue with weird splits for me)
Don't loop on eof
. You're trying to create n
files, so make this a
loop that creates n
for (int i = 0; i < n; ++i)
At the beginning of this
loop would be a good place to generate a new random file name.
By the way, at least on my OS, the '*' (asterisk) character isn't a valid character in a file name, so you should probably remove that from your alphanum
string (speaking of which, you can get the size of it by calling strlen
-- don't rely on
-- Instead of
fc >> letter
will eat the whitespace, and you probably don't want that.
Same thing down below when you write to your "extra" file.
Not sure what this is for, but you don't need it.
-- By the way: you should probably check to make sure that you actually have a file open.