Solved

writing to files from different functions

Posted on 2002-07-10
1
189 Views
Last Modified: 2010-04-01
I am opening a file in main func using

ofstream atminit ("atminit.txt");
if (! atminit.is_open())
{
cout << "error opening atminit.txt, press any key to  exit.....";
getch();
exit (1);
}
else
{
cout << "opened atminit file ok" << endl;
}

I want to write some information to the file from a different function.  Is it possible to open the file globally?  can I pass a pointer of the file to the function and write to the pointer?  If so then how do I open it as a pointer and how do I write to it.

Thanks
0
Comment
Question by:Karls
1 Comment
 
LVL 4

Accepted Solution

by:
mblat earned 60 total points
ID: 7144808
void write(ofstream& atminit)
{
    atminit << "test write:";
}


void main()
{
    ofstream atminit ("atminit.txt");
    if (! atminit.is_open())
    {
        cout << "error opening atminit.txt, press any key to  exit.....";
//        getch();
        exit (1);
    }
    else
    {
        cout << "opened atminit file ok" << endl;
        write(atminit );
    }
}

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question