random number generation to text file

Hi all!

I've posted a similar topic in the beginner forums, I need to generate random numbers, and input them into 'feherzaj.txt' file, my code so far is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//white noise generator

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace 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!
Last edited on
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(...
Thank you a lot, remove checked, and it works nicely ;-)
Topic archived. No new replies allowed.