I've been trying to convert my code from using iostream to fstream but seems the output file is always empty..

this is my original code

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;


struct RationalNo
{
       int RatNo [20];
};

struct RationalInfo
{      
       RationalNo rational;
       int quotient;
       int remiander;  
       float value;
};

int main()
{
    srand(time(NULL));
    
    RationalNo intNum;
    RationalInfo modNum;
    
    cout << "Original Integers" << endl;
    
    for (int i = 0; i < 20; i++)//Array Generation
        {
             intNum.RatNo [i] = rand() % 101 + 1;
             cout << intNum.RatNo [i] << "\t";
		
        }
    cout << endl;

    cout << "Quotients of integers" << endl;//Quotient
    for (int n = 0; n < 20; n++)
        {
             modNum.quotient = intNum.RatNo [n] / 10;
             cout << modNum.quotient << "\t";             
             
        }
    cout << endl;

    cout << "Remainders Of Integers" << endl;//Remainder
    for (int m = 0; m < 10; m++)
        {
             modNum.remiander = intNum.RatNo [m] % 10;
             cout << modNum.remiander << "\t";
        }
    cout << endl;
    
    cout << "Actual Value of integer division" << endl; //value of division
    for (int q = 0; q < 10; q++)
        {
             modNum.value = intNum.RatNo [q] / 10;
             cout << setiosflags (ios::fixed);
             cout << setiosflags (ios::showpoint);
             cout << setprecision (2);
             cout << modNum.value << "\t";
        }
        cout << endl;
}

and this is my fstream attempt

#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;


struct RationalNo
{
       int RatNo [20];
};

struct RationalInfo
{      
       RationalNo rational;
       int quotient;
       int remiander;  
       float value;
};

int main()
{
    srand(time(NULL));
    
    ofstream outfile("Rational.txt");
    ifstream infile("Rational.txt");
    RationalNo intNum;
    RationalInfo modNum;
    
    outfile.open("Rational.txt");
    outfile << "Original Integer" << endl;  
    outfile.close();
    
    outfile.open("Rational.txt");
    for (int i = 0; i < 100; i++)//Array Generation
        {
             intNum.RatNo [i] = rand() % 101 + 1;
             outfile << intNum.RatNo [i] << "\t";
		
        }
    outfile << endl;
    outfile.close();
    
    outfile.open("Rational.txt");
    outfile << "Quotients of integers" << endl;//Quotient
    outfile.close();
    
    outfile.open("Rational.txt");
    for (int n = 0; n < 100; n++)
        {
             modNum.quotient = intNum.RatNo [n] / 10;
             outfile << modNum.quotient << "\t";             
             
        }
    outfile << endl;
    outfile.close();
    
    outfile.open("Rational.txt");
    outfile << "Remainders Of Integers" << endl;//Remainder
    outfile.close();
    
    outfile.open("Rational.txt");
    for (int m = 0; m < 100; m++)
        {
             modNum.remiander = intNum.RatNo [m] % 10;
             outfile << modNum.remiander << "\t";
        }
    outfile << endl;
    outfile.close();
    
    outfile.open("Rational.txt");
    outfile << "Actual Value of integer division" << endl; //value of division
    outfile.close();
    
    outfile.open("Rational.txt");
    for (int q = 0; q < 100; q++)
        {
             modNum.value = intNum.RatNo [q] / 10;
             outfile << setiosflags (ios::fixed);
             outfile << setiosflags (ios::showpoint);
             outfile << setprecision (2);
             outfile << modNum.value << "\t";
        }
    outfile << endl;
    outfile.close();
}

Why do you keep opening and closing your outfile? (You never do that with cin...)

Get rid of every line that says outfile.open( ... ); and every line except the last that says outfile.close(); , and try it again.

Hope this helps.

Every time you open the output file, by default it gets truncated (content deleted.) As Duoas said, there's no need to repeatedly open and close it. Your computer will waste more time just doing that than actually doing the work of the program.

This isn't so much to help solve your problem but a suggestion.

You don't need two different versions for cout and a file. Use an ostream. This will allow you to then direct the the output to any ostream derived object. This includes cout, ofstream, or even to a network stream, such as the one in asio (http://asio.sourceforge.net/).

Consider something like this: create a function that takes a reference to an ostream. Send all of your output in that function.

void dosomething(ostream& out);
// In here do this: out << "Some Text" << endl;

Now you can use that function like this: dosomething(cout); or

ofstream outfile("Rational.txt");
dosomething(outfile);

Additionally, don't use close() in dosomething. If you want to close the file, do it after calling dosomething. But you don't even have to do that. The file will automatically be closed when it's lifetime ends. In your example, outfile will be closed when the end of main() is reached. You may have a reason to close it yourself, however.

hmm i did Duoas suggestted but when i execute my cmd window hangs and my output file only displays the first line of my output

#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;


struct RationalNo
{
       int RatNo [20];
};

struct RationalInfo
{      
       RationalNo rational;
       int quotient;
       int remiander;  
       float value;
};

int main()
{
    srand(time(NULL));
    
    ofstream outfile("Rational.txt");
    ifstream infile("Rational.txt");
    RationalNo intNum;
    RationalInfo modNum;
    
    
    outfile << "Original Integer" << endl;  
    
    
   
    for (int i = 0; i < 100; i++)//Array Generation
        {
             intNum.RatNo [i] = rand() % 101 + 1;
             outfile << intNum.RatNo [i] << "\t";
		
        }
    outfile << endl;
    
    
    
    outfile << "Quotients of integers" << endl;//Quotient
    
    
    
    for (int n = 0; n < 100; n++)
        {
             modNum.quotient = intNum.RatNo [n] / 10;
             outfile << modNum.quotient << "\t";             
             
        }
    outfile << endl;
    
    
    
    outfile << "Remainders Of Integers" << endl;//Remainder
   
    
    
    for (int m = 0; m < 100; m++)
        {
             modNum.remiander = intNum.RatNo [m] % 10;
             outfile << modNum.remiander << "\t";
        }
    outfile << endl;
    
    
    
    outfile << "Actual Value of integer division" << endl; //value of division
    
    
    
    for (int q = 0; q < 100; q++)
        {
             modNum.value = intNum.RatNo [q] / 10;
             outfile << setiosflags (ios::fixed);
             outfile << setiosflags (ios::showpoint);
             outfile << setprecision (2);
             outfile << modNum.value << "\t";
        }
    outfile << endl;
    
}

All the for loops use an upper limit of 100 but you only have 20 elements in intNum.RatNo[], that's why it fails.

This article has been dead for over six months. Start a new discussion instead.