Solved

writing to files from different functions

Posted on 2002-07-10
1
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 515
thread-safe code in c++ 2 120
computer science syllabus 3 100
gdb doesn't stop on breakpoint 2 113
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

740 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