Solved

writing to files from different functions

Posted on 2002-07-10
1
192 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 73
How to split this in C++ 4 106
Eclipse IDE - Cannot copy/paste from console output 8 253
Gaming Software 1 19
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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