Reading and writing to files

I'm new to C++. I use Visual Studio 2019. I am trying to program a simple program that simulates a sample of normal random variables by reading the parameters (mean and stdev) from a .txt file and writes the simulated output to a .txt file.
I have placed the two data (.txt) files in the sample Project Folder as the attached code.

[code]
// ReadFile3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <random>
#include <iostream>
#include <fstream>
using namespace std;
using std::cout;

ifstream params;
ifstream nreps;

ofstream out1;

void test(const double m, const double s, const int samples) {

//uncomment to use a non-deterministic seed
// random device gen;
// mt19937 gen (rd());
mt19937 gen(1701);

normal_distribution<> distr(m, s);
}

int main()
{
double m_dist = 1;
double s_dist = 1;
double data2[2];
int data1[1];
int samples;

params.open("C:\\Cpp\\VSProjects\\ReadFile3\\SimParams.txt");
nreps.open("C:\\Cpp\\VSProjects\\ReadFile3\\nreps.txt");

out1.open("C:\\Cpp\\VSProjects\\ReadFile3\\samples.txt");

for (int i = 0; i < 2; i++) {
params >> data2[i];
}
m_dist = data2[0];
s_dist = data2[1];

for (int i = 0; i++;) {
samples = data1[i];
}

test(m_dist, s_dist, samples);

out1 << test;
out1.close();

}

I would greatly benefit from having someone simply write the correct code I am trying to program here.
Edit your post and add [/code] at the end of your code so that it formats it.

1
2
3
for (int i = 0; i++;) {
    samples = data1[i];
}
What are you attempting to do here? It doesn't make sense.

Does your params file only consist of two numbers? (presumably, mean and stddev)
I would give your variables better names. Instead of "data2", have one variable called mean, and another variable called stddev.
Then do
1
2
3
4
5
6
double mean;
double stddev;
if (!(params >> mean >> stddev))
{
    std::cout << "Error reading params\n";
}


For your normal_distribution, I would just initialize one mt19937 generator at the beginning of main. Pass it into your functions to use it. You don't want to keep re-seeding the generator.

You need to actually do something with your normal_distribution object. For example,
double value = distr(gen);
Last edited on
Thanks for the suggestions. MY apologies for the description of what I am trying to do. Here's another try.
If I were in Matlab, I would assign values to mean, stdev, and nreps (number of values from the distribution to simulate. Then in a script or at the Command line I would write something like this:

X = normrnd(mean, stdev, nreps) or
X = normrnd(5.0, 1.5, 10000)
Then I could save X (containing 10000 random normals) to an Excel file or to a text file.

I want to do the same in C++. I want to have a text file that contains three numbers: mean, stdev, and nreps (for example, 5.0, 1.5, 10000). I want to read these numbers in from the .txt file, assign the first number to 'mean, the second to 'stdev', and the third to 'nreps, simulate nreps normal randoms with mean 'mean' and standard deviation 'stdev', then output the 10000 simulated values to a text file.

I would like suggestions as to the simplest and most efficient way to do this.

Nick
You should still fix your code tags (by adding the ending [/code] tag) in your original post.

You might do it something like this. This is assuming that you actually have one text file for the mean and stddev (separated only by whitespace) and another text file for the repetitions, which seems odd, but that's what your original code implies.

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
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <random>
#include <string>
using namespace std;

int main()
{
    string dir{"C:/Cpp/VSProjects/ReadFile3/"};
    ifstream f_params(dir + "SimParams.txt");
    ifstream f_nreps(dir + "nreps.txt");
    ofstream f_out(dir + "samples.txt");
    if (!f_params || !f_nreps || !f_out) {
        cerr << "Problem opening the files.\n";
        return 1;
    }

    double mean, stddev;
    if (!(f_params >> mean >> stddev)) {
        cerr << "Can't read mean and/or stddev.\n";
        return 1;
    }

    int nreps;
    if (!(f_nreps >> nreps)) {
        cerr << "Can't read nreps.\n";
        return 1;
    }

    normal_distribution<> dist(mean, stddev);
    random_device rd{};
    mt19937 gen(rd());
    for (int i = 0; i < nreps; ++i)
        f_out << dist(gen) << '\n';
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <random>
#include <ctime>
using namespace std;

mt19937 gen( time( 0 ) );
normal_distribution<double> Z;     // standard normal Z ~ N(0,1)

int main()
{
   ifstream in( "SimParams.txt" );
   ofstream out( "samples.txt" );

   int N;
   double mu, sigma;
   in >> mu >> sigma >> N;
   while( N-- ) out << mu + sigma * Z( gen ) << '\n';
}
Thanks to dutch and last chance. This helped me greatly!
Topic archived. No new replies allowed.