[Dev-C++] Writing and Reading from File in separate "Classes"
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ben <CPl...@be...> - 2011-09-29 12:21:42
|
Hi,
its been some time since I have used C++ and due to that I might use some
wrong vocabulary, sorry for that. I do however hope that I can bring my
question across. And I guess its going to be rather easy but I have not fund
a solution via google. Thanks for your help!
In the program I am going to write I will have to read and write data from a
number of files. Rather then to write the code for this over and over again
I would like to brake it down in smaller peaces (as I understand it that is
also good practice in any way). Below I have included a scheme for the
general idea and below that my code so far.
1. Program starts
2. Subroutine "Open/Close" is called with a file name as a char[]
3. "Open" returns the open file
4. Data is written to the file
5. Subroutine "Open/Close" is called and closes the file
Now to what code I cam up with:
#include <fstream>
void NewFile(bool OpenClose, const char FileNameExt, const char FileNameInt)
// If OpenClose = TURE the file is opened and if it is FALSE it is closed.
Any previouse File content will be deleted!
{
if (OpenClose==true)
{
std::ofstream FileNameExt (FileNameInt, std::ios::trunc);
FileNameInt << "Test\n";
}
if (OpenClose==false)
{
FileNameInt.close();
}
}
int main()
{
NewFile(true, "LogFile", "LogFileInt");
FileNameInt << "Please Work!\n";
NewFile(false, "LogFile", "LogFileInt");
}
Thanks for any help,
Ben
|