Thread: Problem writing to external file

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Problem writing to external file

    I am attempting to write a program for an assignment. I have to use several functions to open and then write some data to an external file. I have to use one function just to open the external file and then another function to write a header with columns to the file. It is a requirement that we allow the user to name the file. Here is my difficulty; I was able to allow the user to name the file and then open it, but it doesn't write the data to the file. Here is my code.
    Code:
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    #include <cstdlib>     
    #include <fstream>     
    #include <iostream>
    
    using namespace std;
    
    int openOutputFile(char n[]);
    void writeHeaderLinesToOutputFile();
    int createAndSaveLoanData();
    double calculateOneLineOfLoanResults();
    double calculateMonthlyPayment(double r, double expm, double yrs);
    double calculateTotalPayment(double mPmt);
    int writeLineOfResultsToOutputFile();
    
    int main()
    {
        
       
        ofstream outFile;    
        char outputName[50];
       
        cout << "Please type the name of the output file, including the extension: ";
        cin >> outputName;
        openOutputFile(outputName);
        
        cin.get();
        return 0;
    }
    int openOutputFile(char name[50])
    {
        
        ofstream outFile; 
        
        outFile.open(name);
            if (outFile.fail())
            {
                cerr << "Could not open " << name << "!" << endl;
                return EXIT_FAILURE;
            }
        writeHeaderLinesToOutputFile();    
        return 0;
    }
    void writeHeaderLinesToOutputFile()
    {
        ofstream outFile; 
        
              outFile << "Loan Amount: $1000" << '\n'; 
              outFile << left << setw(9) << "Interest" << left << setw(9) << "Duration" << left << setw(9)  << "Monthly"; 
              outFile << left << setw(9) << "Total" << '\n';
              outFile << left << setw(9)  << "rate" << left << setw(9) << "(years)" << left << setw(9) << "payment";
              outFile << left << setw(9) << "payment" << '\n';
              outFile.close();
    }
    Can someone tell me what I am going wrong? I am pretty sure the problem is occurring because the file is being opened in one function and written to in another, but I have no idea how to get around it.
    Last edited by kristentx; 09-06-2007 at 01:45 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am pretty sure the problem is occurring because the file is being opened in one function and written to in another, but I have no idea how to get around it.
    You need to pass around the ofstream by reference. It could be something like:
    Code:
    int openOutputFile(char name[50])
    {
        ofstream outFile(name);
        if (outFile.fail())
        {
            cerr << "Could not open " << name << "!" << endl;
            return EXIT_FAILURE;
        }
        writeHeaderLinesToOutputFile(outFile);
        outFile.close();
        return 0;
    }
    void writeHeaderLinesToOutputFile(ofstream& outFile)
    {
        outFile << "Loan Amount: $1000" << '\n'; 
        outFile << left << setw(9) << "Interest" << left << setw(9) << "Duration" << left << setw(9)  << "Monthly"; 
        outFile << left << setw(9) << "Total" << '\n';
        outFile << left << setw(9)  << "rate" << left << setw(9) << "(years)" << left << setw(9) << "payment";
        outFile << left << setw(9) << "payment" << '\n';
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your outFile variables are local to the functions they are declared in. They are two different variables. Inside the openOutputFile function, you open the output file successfully, but when the function ends the outFile variable is destroyed. When an fstream is destroyed it closes the file.

    Later, in your writeHeaderLinesToOutputFile function, you create a new variable called outFile. Then you don't open a file there, you just try to write to it, which of course fails.

    The solution to this problem is to create the ofstream variable in main and pass it as an argument to the two functions, then have those functions work on it instead of a local variable. Since you will be modifying the ofstream, make sure to pass it by reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with writing in a file using fprintf
    By g_p in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 08:51 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM