LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2002, 10:16 PM   #1
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Rep: Reputation: 30
a class with a ifstream member


Hi,

im having problems because i want to do a class with a ifstream member variable, but given the sintax for calling a ifstream:

ifstream blaj (filename, size);

then im not sure about what im trying to do

class file_manager {
ifstream file;
//methods go here
....
};
it just compile fine (on icc) but then i try to open it

file_manager test;
ifstream test.file (flahh...);

and it just doesn't seem to like it,
is there any other way of doing this?

the reason i want a ifstream on a class is because i want to open arrays of files, so it looks like this:

file_manager filearray[50];
for( i=0 ; i < 50 ; i++) {
filearray[i].getline(buffer,256);
cout << "reading from file "<<i<<" i found "<<buffer<<endl;
};


any ideas how to do this?

Hano
 
Old 04-24-2002, 06:33 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
I think you are mixing up a few things there. I'm not totally clear on what you want to do. But the best way is to either make a class that handles everything for you or just make an array of ifstreams without using a class.

To do it with a class you could do something like this:
Code:
class file_manager
{
public:
  file_manager();
  ~file_manager();

  bool open(char* name);
  char* getline();
  bool close();

private:
  ifstream m_file;
};
Ofcourse you'll have to implement all the contents of those functions yourself but most of them will be just simply calling the members of ifstream.

To do it without a class you could do something like this:
Code:
ifstream filearray[50];
for( i=0 ; i < 50 ; i++)
{
  filearray[i].open(filename[i]);
  filearray[i].getline(buffer,256);
  cout << "reading from file " << i
          << " data: " << buffer << endl;
}
 
Old 04-24-2002, 11:01 AM   #3
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Original Poster
Rep: Reputation: 30
Thanx Mik!

that was just what i was trying to do, but the key was the open() member, that it doesnt appear documented nowhere...
maybe because nobody assumes you need not to open on declaration

Hano
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
LQ member progress status from member to senior member............. emailssent LQ Suggestions & Feedback 3 10-11-2004 01:31 PM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration