Saving results to a file in C++

Posted by user1680877 on Stack Overflow See other posts from Stack Overflow or by user1680877
Published on 2012-10-28T22:15:45Z Indexed on 2012/10/28 23:01 UTC
Read the original article Hit count: 217

Filed under:
|
|

I have a problem with this code.

What I am looking for in the code is to get the result of "first" and "second" randomly and put the result in a file.

It works great if I run it without using the file and I get all the correct results, but when I try to save the result in the file, I get only the first node which contains (first, secnd).

Here is the code:

#include<iostream>
#include <fstream>
#include<cmath>

using namespace std;

void main() 
{
    int first[100],secnd[100];

    for (int i=0; i<100 ;i++)
    {   
        first[i]=rand()%500;  //random number from  to 499
        secnd[i]=rand()%500;  //random number from  to 499
        ofstream myfile;
        myfile.open ("example.txt");
        myfile << "Writing this to a file.\n";
        myfile <<first[i]<<" "<<secnd[i];
        myfile.close();
    }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-c++