C++ Output to file is incorrect?

Viewed 65

This program is to generate numbers between 0 - 232, and I am trying to copy this randomly generated sample into a file; but it gives output as a string of multiple equal signs (=). I don't understand where the error can be from.

#include<iostream>
#include<conio.h>
#include <ctime>
#include<fstream>
#include <cstdlib>
#include <iomanip>
#include <math.h>
#include <vector>
#include <random>
#include <string>

using namespace std;

int main(){

    int ya;
    vector<unsigned long long> instance;
    fstream inFile;
    inFile.open("outputFile.txt");

    if (!inFile) {

        cout << "The file can't be opened" << endl;
    }

    std::mt19937_64 generator;
    generator.seed(std::time(0));
    uniform_int_distribution<unsigned long long>randomNumber(0, 4294967295);

   
    for (int index = 0; index < 10; index++) {
      cout << randomNumber(generator) << endl;
       inFile << randomNumber(generator)<<"\n";
       //instance.push_back(randomNumber(generator));
    }


   
   
    if (inFile.is_open()) {

        cout << inFile.rdbuf();
    }

   
    return 0;
}
2 Answers

There are a few issues in your code. First, you should open the file explicitly in in|out mode. Second, you are generating two different random numbers in each loop: one for display with cout and another for writing to the file. And third, you need to rewind your file before reading its contents.

Here's a trimmed-down and corrected version of your code:

#include <iostream>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <random>
using namespace std;

int main()
{
    fstream inFile;
    inFile.open("outputFile.txt", ios_base::out|ios_base::in); // Open for OUTPUT and input!
    if (!inFile) {
        cout << "The file can't be opened" << endl;
        return -1; // Error - no point continuing!
    }
    std::mt19937_64 generator;
    generator.seed(std::time(0));
    uniform_int_distribution<unsigned long long>randomNumber(0, 4294967295);
    for (int index = 0; index < 10; index++) {
        unsigned long long number = randomNumber(generator); // Save the number so same is displayed and stored!
        cout << number << endl;
        inFile << number << "\n";
    }
    // We shouldn't need this check, because of the earlier " if (!inFile) " check.
    if (inFile.is_open()) {
        cout << "File contents:" << endl;
        inFile.seekg(0); // REWIND the file before reading its contents!
        cout << inFile.rdbuf();
        inFile.close();
    }
    else { 
        cout << "file is not open";
    }
    cout << endl;
    return 0;
}

One further note: the name inFile for the variable used to store output is bizarre - but it won't affect the code.


EDIT: Following the very useful comment by user4581301, here's a version that will work whether or not the "outputFile.txt" file already exists:

int main()
{
    fstream inFile;
    inFile.open("outputFile.txt", ios_base::out); // Open for OUTPUT and truncate
    if (!inFile) {
        cout << "The file can't be opened for writing!" << endl;
        return 0;
    }
    std::mt19937_64 generator;
    generator.seed(std::time(0));
    uniform_int_distribution<unsigned long long>randomNumber(0, 4294967295);
    for (int index = 0; index < 10; index++) {
        unsigned long long number = randomNumber(generator); // Save the number so same is displayed and stored!
        cout << number << endl;
        inFile << number << "\n";
    }
    inFile.close();
    inFile.open("outputFile.txt", ios_base::in); // Open for INPUT (implicit rewind)
    if (inFile.is_open()) {
        cout << "File contents:" << endl;
        cout << inFile.rdbuf();
        inFile.close();
    }
    else {
        cout << "The file can't be opened for reading!";
    }
    cout << endl;
    return 0;
}
#include<iostream>
#include<conio.h>
#include <ctime>
#include<fstream>
#include <cstdlib>
#include <iomanip>
#include <math.h>
#include <vector>
#include <random>
#include <string>

using namespace std;

int main() {

int ya;
vector<unsigned long long> instance;
fstream inFile;
inFile.open("outputFile.txt", ios_base::out);
if (!inFile) {
cout << "The file can't be opened" << endl;
}
std::mt19937_64 generator;
generator.seed(std::time(0));
uniform_int_distribution<unsigned long long>randomNumber(0, 4294967295);
for (int index = 0; index < 10; index++) {
cout << randomNumber(generator) << endl;
inFile << randomNumber(generator) << "\n";
//instance.push_back(randomNumber(generator));
}
if (inFile.is_open()) {
inFile.close();
}
return 0;
}
Related