Solved

writing to files from different functions

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now