473,385 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

one fstream member instead of ofstream and ifstream - acceptable practice?

175 100+
I have a class that does file handling. This class will be inherited and perform several file handling functions. So, I want private members for fstream. I originally thought of making two members - one for ifstream, and one for ofstream, but am now considering just having one member and making it an fstream.

From a design perspective, is this generally accepted, or is it considered bad form?

so, instead of

Expand|Select|Wrap|Line Numbers
  1. private:
  2.     ofstream m_outfile;
  3.     ifstream m_infile;
  4.  
it would be

Expand|Select|Wrap|Line Numbers
  1. private:
  2.     fstream m_file;
  3.  
Apr 10 '12 #1
1 1587
weaknessforcats
9,208 Expert Mod 8TB
Depends upon whether you need an ifstream and an ofstream open at the same time.

Use the fstream to start out.

BTW:m_file does not indicate a member variable. Naming conventions don't work because the compiler can'e enforce them. I would drop the m_ and having pointer names start with p.

Use the scope resolution operator instead:

file -> local variable in the code block
this->file -> file is a member variable

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.    fstream file;
  4.    void MethodA();
  5. };
  6. void MyClass::MethodA()
  7. {
  8.     fstream file   //the local variable
  9.     this->file      //the class member variable
  10.     MyStuff::file   //a variable in the MyStuff namespace
  11.     ::file          //a global variable
  12. }
  13.  
Apr 10 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
by: mark | last post by:
The problem with this code snippet is that only outputs the first word in the file "test". The file has atleast one line with 4 words in it, only one word is output. I have tried increasing the...
6
by: ma740988 | last post by:
There's no way to use the STL algorithm copy to print an outfile (essentially an ofstream)? So now: int main() { std::ifstream InFile( "exercise15.txt"); std::ofstream ToFile( "NewFile.txt"...
4
by: Falos425 | last post by:
Okay, I've got a driver.cpp, a section.cpp, and a section.h because I've got a 'section' class. I have an #include <fstream> in the section.h and then section.cpp has an #include "section.h" ...
5
by: wobudui | last post by:
Hi everyboday, I have some trouble in dealing with the file stream. My souce code Listed hear: int main() { char buffer={0}; ofstream ofile.open("mydata.in",ios::app); ofile.seekp(10);...
4
by: manontheedge | last post by:
i'm working on a C++ program that uses operator overloads (which i'm just learning to use), and i have the ostream working fine, but i'm thinking i'm using it wrong somehow because the ofstream...
6
by: Gary Wessle | last post by:
hi I have a code, the part which is troubling goes like this **************************************************************** #include <istream> #include <ostream> #include <fstream>
7
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
7
by: D. Susman | last post by:
Hi, In the code snippet below, I am trying to read the contents of a file into a vector. However I can't get it compiled. I am using Sunstudio which compiles C++ using cc. Here is the code:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.