0

I want to preface this by saying this is the first assignment in a 102 csc course. My 101 teacher wasn't the best, so I don't have that good of a foundation.

I have an assignment which requires every action to be in its own function. I have to generate random numbers, then write these numbers to a file, and then a few other things. I have the file created and written to. However, I am having a problem where I can't read the values from the file into an array. They are all still 0 when output to the console. I am sure it is something I'm doing wrong with the functions, but I have no problem taking any criticism. I will include the code below. The function I am working in now is named void read().

#include <iostream> 
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <cstddef>
using namespace std;

fstream randomData;
void randomgenerator();
void read(fstream &randomdata);

int main() {    
    randomgenerator();
    read(randomData);   
    return 0;
}

void randomgenerator() {
    srand(time(0));
    randomData.open("randomData.txt");
    for (int counter = 0; counter < 100; counter++) {
        randomData << rand() % 100+1 << endl;
    }
}

void read(fstream &randomData) {
    int numarray[100] = {};
    for (int i = 0; i < 100; i++) {
        randomData >> numarray[i];
        cout << numarray[i];
    }
}

Thank you for taking the time to give input. The std namespace is used because the professor wants us to. I understand it is not efficient.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    You are reading from the same `fstream` you write to, but you are not seeking the `fstream` back to the beginning of the file after writing to it, so you are reading from the end of the file, causing `operator>>` to fail which you don't check for. I would suggest using `std::ofstream` inside of `randomgenerator()`, and then use a separate `std::ifstream` inside of `read()` – Remy Lebeau Jan 29 '20 at 23:30
  • **WARNING**: Using [`rand()` is highly problematic](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) and you’re strongly encouraged to use an appropriate [random number generator facility in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random) that produces high-quality random values. Your use of `time(NULL)` as a random number seed means that this will produce identical results if run in the same second, and on many platforms `rand()` is [*barely* random at all](http://dilbert.com/strip/2001-10-25). – tadman Jan 29 '20 at 23:33
  • 1
    `The std namespace is used because the professor wants us to. I understand it is not efficient.` Actually, [it's worse than just inefficient](https://stackoverflow.com/q/1452721/10957435). –  Jan 29 '20 at 23:37
  • The first thing you need to do is pass the class, so give the professor what they're asking for. Just know that sometimes they ask you to do silly things. Some of these silly things are [Wax-on Wax-off](https://www.youtube.com/watch?v=fULNUr0rvEc) things where you'll figure out the point later and some are just silly. Good luck figuring out which are which right now. – user4581301 Jan 29 '20 at 23:42
  • ok @RemyLebeau That makes alot of sense. So if I copy the ofstream I opened at first into an ifstream, I shouldn't have a problem reading from the ifstream correct? – killerkody gaming Jan 29 '20 at 23:43
  • 1
    @killerkodygaming You can't copy an `ofstream` to an `ifstream`. You would open an `ofstream` to the filename, write to it, and close it. Then open an `ifstream` to the same filename, read from it, and close it. Or, if you want to reuse the same `fstream` for writing and reading, just call [`seekg(0)`](https://en.cppreference.com/w/cpp/io/basic_istream/seekg) on the stream before reading from it. – Remy Lebeau Jan 29 '20 at 23:57
  • @RemyLebeau I was thinking about it during my workout and that dawned on me that I only needed to close and then open it. Thanks for the help. I think ive got it now. – killerkody gaming Jan 30 '20 at 03:54

1 Answers1

0

This was the solution for me, hope this helps any future searches for this problem. The solution was to create another variable with datatype ifstream. This variable accessed the same file, but allowed me to read from it instead of writing to it. I had to close the file from the randomData variable, and open the inputrandomData variable. Below is the revised code.

using namespace std;
ofstream randomData;
ifstream inputrandomData;
void randomgenerator();
void read();
int main() {

    randomgenerator();
    read();

    return 0;
}


void randomgenerator() {
    srand(time(0));
    randomData.open("randomData.txt");
    for (int counter = 0; counter < 100; counter++) {
        randomData << rand() % 100+1 << endl;
    }
    randomData.close();

}


void read() {
    inputrandomData.open("randomData.txt");
    int numarray[100] = {};
    for (int i = 0; i < 100; i++) {
        inputrandomData >> numarray[i];
        cout << numarray[i];
    }


}

Thanks for the assistance folks!

  • 1
    You should move the `ofstream` variable inside of `randomgenerator()`, and move the `ifstream` variable inside of `read()`. Stay away from using global variables when you can help it. – Remy Lebeau Jan 30 '20 at 04:53