Quick question regarding writing to outfile

Mar 18, 2012 at 4:58pm
Hello all,
I was just wondering why this function does not write the header to the output file? The cout works fine, but it isn't writing to the output file. What am I missing or doing wrong? Also, this is the first time I've passed the input/output stuff, so am I doing that correctly? Well, I guess I'm not, or it would be working, right?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>  //Input/output files
#include <string>   //For strings
#include <sstream>  //For stringstream()
#include <iomanip>  //For setw()

using namespace std;

//Declare constants
const int MAX_RECORDS = 6;
const int MAX_DATES = 150;

//This is a hierarchical struct since Date is a struct type within member list of MachineRecord
struct Date
{
    int month;
    int day;
    int year;
};

struct MachineRecord
{
    string machineNbr;
    string name;
    Date   InstallationDate;
    int    everyXDays;
    int    serviceTimeInDays;
    Date   workDays[MAX_DATES];
};

//Create an array of machine records
MachineRecord pMSchedule[MAX_RECORDS];

//Function prototypes
void DisplayHeader(ofstream& outfile);
int OpenInputOutput(ifstream& infile, ofstream& outfile);

int main()
{
    //File descriptors
    ifstream infile;
    ofstream outfile;

    DisplayHeader(outfile);
    OpenInputOutput(infile, outfile);


    //Close input/output files
    infile.close();
    outfile.close();

    return 0;
}

//*************************************************************************************************
//Function displays my class and personal info
void DisplayHeader(ofstream& outfile)
{
    cout << "Erin Corona" << endl
         << "CS 318.20181" << endl
         << "Scheduler Time Maintanence Modified Again - Program 12" << endl
         << "Due Tuesday, March 20, 2012" << endl << endl;
    outfile << "Erin Corona" << endl
         << "CS 318.20181" << endl
         << "Scheduler Time Maintanence Modified Again - Program 12" << endl
         << "Due Tuesday, March 20, 2012" << endl << endl;
}//End of function

//************************************************************************************
//Function opens input and output files.
int OpenInputOutput(ifstream& infile, ofstream& outfile)
    {
        infile.open("machines.txt"); //Open text file to read from
        outfile.open("machinesAgain.out"); //Open textfile to write to
        if (!infile) //If input file did not open correctly
        {
            cout << "Error opening input file." << endl;//Display error message
            outfile << "Error opening input file." << endl;//Display error message
            return 1; //Quit
        }
        else
            {
                cout << "Input file opened correctly." << endl;//Display success message
                outfile << "Input file opened correctly." << endl;
            }

        if (!outfile) //If outfile does not open
        {
            cout << "Error opening output file." << endl;//Display error message
            outfile << "Error opening output file." << endl;//Display error message
            return 1; //Quit
        }
        else
            {
                cout << "Output file opened correctly." << endl << endl << endl;//Display success message
                outfile << "Output file opened correctly." << endl << endl << endl;
            }

    }//End of function 


The cout looks like this:
Erin Corona
CS 318.20181
Scheduler Time Maintanence Modified Again - Program 12
Due Tuesday, March 20, 2012

Input file opened correctly.
Output file opened correctly.



But the output file only does this:
Input file opened correctly.
Output file opened correctly.


All I'm asking is how do I get the header stuff (my name and class info) to show up in the output file?

Thanks for your help.
Mar 18, 2012 at 5:07pm
You have to open the outfile before you try to write to it.
Mar 18, 2012 at 5:10pm
Lol. That explains why I had those functions swapped in the previous program. What would I do without you, Moschops?
Thank you.
Mar 18, 2012 at 5:11pm
Oh, wait, before I call this one solved, did I pass the input/output stuff correctly? Is that how it should look for other functions I haven't written yet too?
Mar 18, 2012 at 5:14pm
closed account (zb0S216C)
I just tested your program. It seems your program is dependent on machines.txt. Without that file within the same directory as your program, your program fails.

To solve this issue, create a file called machines.txt within the same directory as your program. Your program doesn't create this file, so you have to do it manually.

Testing Info:

OS: Ubuntu
Compiler: GCC 4.4.2

Wazzak
Last edited on Mar 18, 2012 at 5:15pm
Topic archived. No new replies allowed.