473,396 Members | 1,785 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,396 software developers and data experts.

How do I use fstream functions in a class?

okay so i want to design a class that will open a text file and manipulate the data in it. and i have to use all these functions from fstream like ifstream, ofstream, seekg etc. the problem is that i can't get the first part to work (getting my constructor to open the file using ifstream). I've posted my test.h file below.



Expand|Select|Wrap|Line Numbers
  1. #ifndef TEST_H_INCLUDED
  2. #define TEST_H_INCLUDED
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8. using std::ifstream;
  9.  
  10. class Test
  11. {
  12. private:
  13.     float hour, minute, second, vrms, irms, time;
  14.     string fileName;
  15. public:
  16.     Test(string);
  17. };
  18.  
  19. Test::Test(string name)
  20. {
  21.     fileName = name;
  22.     ifstream dataFile(fileName);
  23. }
  24.  
  25. #endif // TEST_H_INCLUDED

any help will be appreciated.
Apr 4 '14 #1
2 1413
divideby0
131 128KB
fileName is a std::string, but fstream objects want a const char * as the first argument; use fileName.c_str() or maybe fileName.data()
Apr 5 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
Your ifstream object datafile is a local variable in your constructor. It's deleted when the constructor completes.

This variable should be a private member variable so it can be used by other member functions.
Apr 5 '14 #3

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

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
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();
7
by: Riku Jarvinen | last post by:
Hello everyone, I have a logging class which writes program outputs to the logfile. The class works fine as long as only C++ native data types are considered. The problem is that I have a...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
21
by: Salvatore Di Fazio | last post by:
Hi, I've an array in a class declered in the following way: char pippo; In the constructor of the class I would like to fill this array, but I received an error. Why? tnx
6
by: reon | last post by:
there is a file named student.dat it is saving all the records ... i entered 5 students info into that file using the ofstream.and i just want to get the third students info.... how i can change the...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
1
by: kaens | last post by:
So, I have a class that has to retrieve some data from either xml or an sql database. This isn't a problem, but I was thinking "hey, it would be cool if I could just not define the functions for...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.