Re: [Dev-C++] Writing and Reading from File in separate "Classes"
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: pwm <pw...@ia...> - 2011-09-29 12:53:12
|
Why do you try to have one function with one sepcific name actually do two
completely different thigns?
First off OpenTrue = TURE [sic!] or FALSE isn't a good name. You can't
look at the variable name and know what the fucntion will do.
But why not have:
return_type CreateFile(const char fname[]);
return_type CloseFile();
Next thing - your function creates a local object FileNameExt. First off,
it's a bad name. It is not a file name. So why try to claim that? But then
this is an object that gets destructed when the function ends - so the
caller can't make use of it.
Third - you suddenly use a const char FileNameInt. This is a character.
So how can you take a character variable and think it has a method
close()?
And where would main() get a variable FileNameInt from?
/pwm
On Thu, 29 Sep 2011, Ben wrote:
> 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
>
>
> |