Using external files throughout a program (please read before replying)

D

deanfamily11

I've got a program that uses functions to various tasks easily. How can I
initalize input and output files outside of the main program?
 
S

Shezan Baig

deanfamily11 said:
I've got a program that uses functions to various tasks easily. How can I
initalize input and output files outside of the main program?

Not sure what you mean. Do you mean this?:

#include <iostream>
#include <fstream>

std::ifstream inputFile("input.txt");
std::eek:fstream outputFile("output.txt");

int main()
{
std::cout << "Files have been initialized outside main." <<
std::endl;
return 0;
}


:)
-shez-
 
J

John Harrison

Shezan said:
Not sure what you mean. Do you mean this?:

#include <iostream>
#include <fstream>

std::ifstream inputFile("input.txt");
std::eek:fstream outputFile("output.txt");

int main()
{
std::cout << "Files have been initialized outside main." <<
std::endl;
return 0;
}

Or maybe this

int main()
{
function1();
function2();
return 0;
}

void function1()
{
std::ifstream inputFile("input1.txt");
std::eek:fstream outputFile("output1.txt");
...
}

void function2()
{
std::ifstream inputFile("input2.txt");
std::eek:fstream outputFile("output2.txt");
...
}

Either way its a good idea when asking 'how do I ...' questions to post
the code you have already written. It's often easier to understand what
you want to do then. If you haven't already written some code, then it's
probably good for you to attempt to do so before asking the group.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top