//white noise generator
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
usingnamespace std;
int main() {
srand(time(NULL));
float value=0.0;
ofstream myfile ("feherzaj.txt");
myfile.open ("feherzaj.txt", ios::out | ios::app );
if (myfile.is_open())
{
for (int i = 0; i < 1000; ++i) {
value = rand();
myfile << i << " " << value << "\n";
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
The problem is that it generates the file, which also appears to be opened, because the code haven't triggered else option, but after compiling the files is void, with 0 kbyte, and I can't figure it out why. Thanks for any help!
You are using the second form of the constructor for ofstream which means the file is being created and or opened at construction. So remove the call to myfile.open(.... and you'll find it works. Alternatively you can rather call the first form of the constructor (with no parameters) and then call your myfile.open(...