ofstream
を使用して、内部にテキストが格納されたランダムファイルを生成しています。しかし、生成するファイル数が.txt
であることを指定すると想定される変数fileNum
のように見えます。生成された.txt
ファイルの数には、実際に指定された量との差異があります。時にはその4オフ(または3または2)、そして時々それはそれを正しく取得します。私が間違って何かなりわからないんだけど、私はC++ランダムファイルジェネレータループは正しい量のファイルを生成しません
string genRandStr(int len, int seed) {
// Add a seed, so a different random number is generated each time
srand(time(seed));
char alphaNum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
int strlen = sizeof(alphaNum) - 1;
string randStr = "";
for (int i = 0; i < len; i++) {
randStr += alphaNum[rand() % strlen];
}
return randStr;
}
//main function
int main()
{
int fileNum;
cout << "FILEWARS - SAFE VERSION - USE WITH CARE" << endl;
cout << "Made By: David Yue" << endl;
cout << "Enter the amount of files you wish to create: ";
cin >> fileNum;
if (cin.fail()) {
cout << "Value must be an integer." << endl;
}
if (fileNum >= 30) {
cout << "SAFE VERSION RESTRICTION: File Amount may not exceed 30" << endl;
main();
} else {
ofstream file1;
stringstream fileName;
for (int i = 0; i <= fileNum; i++) {
fileName << genRandStr(6, time(i)) << ".txt";
file1.open(fileName.str());;
for (int i = 0; i < 10; i++) {
file1 << genRandStr(100, time(i)) << "\n";
}
file1.close();
file1.clear();
fileName.str("");
fileName.clear();
Sleep(1000);
}
system("pause");
}
}
'fileNum'の出力を試してみましたか? – John3136
@ John3136私はショットを与えてみましょう –
ええ。私はその問題を@FirstStepから見つけ出し、FileNumは正しく印刷しています。しかし、まだバグが発生しています。 –